Saturday, September 28, 2013

Hot Dogs

     Alright, I think we should tinker around more with objects for practice. I admit, it's getting sort of confusing!
     This time we'll work with meat tubers, or hot dogs.

class hotDog:

     The attributes we will give the hot dog are how long we cooked the hot dog, if it's raw, medium, well-done, or burnt, and what condiments it has.

    def __init__(self):
        self.cookedLevel=0
        self.cookedName="raw"
        self.condiments=["no condiements"]

     Yep, that's a pretty good tasting hot dog right there.
     Well, it may not be cooked or have any condiments, but before we do anything, we should program the levels from raw meat to charcoal.

    def cook(self, time):
        self.cookedLevel=self.cookedLevel + time
        if self.cookedLevel > 8:
            self.cookedName="burnt & black"
        elif self.cookedLevel > 5:
            self.cookedName="perfect & pure"
        elif self.cookedLevel > 3:
            self.cookedName="a well wiener"
        else:
            self.cookedName="raw & repulsive"

     There's our levels. Now, let's cook those dogs!
     Here is the full program. The sleeping time parts are just so the words don't go by so quickly. Here is the full program:

import random, time


class hotDog:
    def __init__(self):
        self.cookedLevel=0
        self.cookedName="raw"
        self.condiments=["no condiments"]

    def cook(self, time):
        self.cookedLevel=self.cookedLevel + time
        if self.cookedLevel > 8:
            self.cookedName="burnt & black"
        elif self.cookedLevel > 5:
            self.cookedName="perfect & pure"
        elif self.cookedLevel > 3:
            self.cookedName="a well wiener"
        else:
            self.cookedName="raw & repulsive"

theDog=hotDog()
print("Your hot dog has been cooked for", theDog.cookedLevel, "minutes.")
time.sleep(3)
print("Your dog is", theDog.cookedName, ".")
time.sleep(3)
print("Your wiener has", theDog.condiments, ".")
time.sleep(2)

print("How about we cook the hot dog? I'll cook it for a random amount of time.")
time.sleep(3)
randomCookTime=random.randint(1, 9)
theDog.cook(randomCookTime)
time.sleep(randomCookTime)
print("I have cooked the weiner for", theDog.cookedLevel, "minutes.")
time.sleep(3)
print("The dog is now", theDog.cookedName, ".")
time.sleep(3)
print("Does it taste better?")


     Alright! Save this in a new program, and run it. This is what I got:

Your hot dog has been cooked for 0 minutes.
Your dog is raw .
Your wiener has ['no condiments'] .
How about we cook the hot dog? I'll cook it for a random amount of time.
I have cooked the wiener for 7 minutes.
The dog is now perfect & pure .
Does it taste better?

     You should get something like that (but you might get a different time at line 5 and a different title at line 6). But, we're not finished just yet! This hot dog has no condiments! Things are about to get trickier...
     Here's the new and improved program:

import random, time


class hotDog:
    def __init__(self):
        self.cookedLevel=0
        self.cookedName="raw"
        self.condiments=["no condiments"]

    def __str__(self):
        msg="A hot dog"
        if len(self.condiments) != "no condiments":
            msg=msg + " with "
        for i in self.condiments:
            msg=msg+i+", "
        msg=msg.strip(", ")
        msg=self.cookedName + " " + msg + "."
        return msg

    def cook(self, time):
        self.cookedLevel=self.cookedLevel + time
        if self.cookedLevel > 8:
            self.cookedName="burnt & black"
        elif self.cookedLevel > 5:
            self.cookedName="perfect & pure"
        elif self.cookedLevel > 3:
            self.cookedName="a well wiener"
        else:
            self.cookedName="raw & repulsive"

def addCondiment(self, condiment):
    self.condiments.append(condiment)

theDog=hotDog()
print("Your hot dog has been cooked for", theDog.cookedLevel, "minutes.")
time.sleep(3)
print("Your dog is", theDog.cookedName, ".")
time.sleep(3)
print("Your wiener has", theDog.condiments, ".")
time.sleep(2)

print("How about we cook the hot dog? I'll cook it for a random amount of time.")
time.sleep(3)
condimentlist=["ketchup", "mustard", "relish", "mayonnaise"]
randomCondiments=random.randint(0, 3)
randomCookTime=random.randint(1, 9)
theDog.cook(randomCookTime)
time.sleep(randomCookTime)
print("I have cooked the weiner for", theDog.cookedLevel, "minutes.")
time.sleep(3)
print("The dog is now", theDog.cookedName, ".")
time.sleep(3)
print("Does it taste better?")
time.sleep(3)
print("Wait, we need condiments!")
time.sleep(3)
print("Let's cook another hot dog...")
theDog.cook(randomCookTime)
time.sleep(randomCookTime)
print("I have cooked another hot dog for", randomCookTime, "minutes.")
time.sleep(3)
print("So, that means the hot dog is", theDog.cookedName, ".")
time.sleep(3)
print("Now, perfect or not, let me put the condiments on it.")
time.sleep(3)
for i in range(randomCondiments):
    theCondiment=i
if theCondiment==0:
    theCondiment="ketchup"
elif theCondiment==1:
    theCondiment="mustard"
elif theCondiment==2:
    theCondiment="relish"
else:
    theCondiment="mayonnaise"
print("Here are the condiments:")
time.sleep(3)
print(theCondiment)

     There is really an easier way to explain this, but I'm going the creative way to make a fun hot dog program that doesn't do the same thing every time.
     When I ran the program, I got this:

Your hot dog has been cooked for 0 minutes.
Your dog is raw .
Your wiener has ['no condiments'] .
How about we cook the hot dog? I'll cook it for a random amount of time.
I have cooked the weiner for 6 minutes.
The dog is now perfect & pure .
Does it taste better?
Wait, we need condiments!
Let's cook another hot dog...
I have cooked another hot dog for 6 minutes.
So, that means the hot dog is burnt & black .
Now, perfect or not, let me put the condiments on it.
Here are the condiments:
ketchup

     Ta-da! Wait...
     When the hot dog was cooked for 6 minutes, it was perfect & pure. I get that, but there's one problem that happened here...
     The wiener was cooked for the same amount of time, and the hot dog ended up burnt & black!
     This article's been going on a bit too long. We'll do some debugging next time!

Sunday, September 8, 2013

__str__()

     __init__(), a __xxxx__() function is a special method that initializes an object when it's created. All objects have an automatic one, so when you don't use __init__(), it will automatically initialize anyway. And then there is __str__(), which let's Python know what you want it to say. What do I mean? Try printing the object in one of our previous programs:

Ball2.py

print(Ball2) -- <class '__main__.Ball2'>
print(theBall) -- <__main__.Ball2 object at 0x02BD9570>

Ball.py

print(Ball) -- <class '__main__.Ball'>
print(theBall) -- <__main__.Ball object at 0x02AD2E90>

     As you saw here, Python told you where the instance is defined (or __main__, A.K.A. the main part of the program), the class name (Ball2/Ball), and where Python stores it in its memory (0x02BD9570/0x02AD2E90).
     If you want Python to say something you actually want to understand, you would use __str__() like this:

class Ball3:
    def __init__(self, color, size, direction):
        self.color = color
        self.size = size
        self.direction = direction

    def __str__(self):
        msg="This ball is a " + self.size + ", " + self.color + " ball."
        return msg

theBall=Ball3("purple", "big", "down")
print(theBall)

     What's with the plus signs (+)? Well, I'm not exactly sure, but you need this for a tuple. Otherwise...

Traceback (most recent call last):
  File "C:/Python33/Ball3.py", line 12, in <module>
    print(theBall)
TypeError: __str__ returned non-string (type tuple)

     Anyway, after debugging it, it should end up doing this:

This ball is a big, purple ball.

     Seems to be a lot of trouble to go through as you can always just do

print("This ball is a big, purple ball.")

     But this way is an option, because you might need to describe the attributes thousands of lines of code later, and you just don't want to go back and look for it if you forget it. That's the beauty of computers: You can forget all you want.
     I think I've rambled on long enough for one article. See ya later!