Thursday, October 31, 2013

Pumpkin Carving

     Let's do a little more object practice before moving on. Since it's Halloween, let's do pumpkin carving!

First, let's make our pumpkin and its properties.

class Pumpkin:
    def __init__(self):
        self.eyes="No eyes"
        self.nose="No nose"
        self.mouth="No mouth"
        self.misc="No extras"
        self.expression="No expression"

     So, basically, this program is where you carve a pumpkin, and you can choose the shape of your face. Let's go the simple way, and start the visual part of the program now. But, first, I want to practice an old thing I wrote a long time ago. Put this at the top of your program:

import easygui, time

     Alright, now you know I want to practice EasyGUI again! Well, here's what I did under the def __init__(self): block:

print("Time to carve some pumpkins!")
time.sleep(4)
print("First things first...")
time.sleep(4)
choices=easygui.buttonbox("How do you want to shape the eyes?", choices=["Triangles", "Circles", "Squares", "Fireballs", "Stars", "Ovals", "Swirls"])

     So, you should get this when you run the program so far:

Time to carve some pumpkins!
First things first...

     Then a window that says:


     Well, once you click a button, the program just ends (since there's no lines after the EasyGUI). Well, just to prove that EasyGUI actually did something, add this to the program:

if choices == "Triangles":
    Pumpkin.eyes='Triangle'
elif choices == "Circles":
    Pumpkin.eyes='Round'
elif choices == "Squares":
    Pumpkin.eyes='Square'
elif choices == "Fireballs":
    Pumpkin.eyes='Fire'
elif choices == "Stars":
    Pumpkin.eyes='Star'
elif choices == "Ovals":
    Pumpkin.eyes='Oval'
else:
    Pumpkin.eyes='Swirly'
print("The pumpkin has", Pumpkin.eyes, "eyes.")

     This is what I got when I picked Swirls.

Time to carve some pumpkins!
First things first...
The pumpkin has Swirly eyes.

     So, there you go. The process repeats for the nose, so here's the rest of the program.

time.sleep(4)
print("Okay, let's do the nose.")
time.sleep(3)
choices=easygui.buttonbox("What shape do you want the nose to be?", choices=["Triangle", "Circle", "Arrowhead", "Fireball", "Swirl", "Square", "Oval", "Star"])
if choices == "Triangles":
    Pumpkin.nose='Triangle'
elif choices == "Circles":
    Pumpkin.nose='Round'
elif choices == "Squares":
    Pumpkin.nose='Square'
elif choices == "Fireballs":
    Pumpkin.nose='Fire'
elif choices == "Stars":
    Pumpkin.nose='Star'
elif choices == "Ovals":
    Pumpkin.nose='Oval'
elif choices == "Arrowhead":
    Pumpkin.nose="Arrowhead"
else:
    Pumpkin.nose='Swirly'
print("The pumpin has a", Pumpkin.nose, "nose.")
time.sleep(4)
print("Now, the mouth! But first...")
choices=easygui.buttonbox("What expression do you want the mouth to have?", choices=["Smile", "Laugh", "Scowl"])
if choices == "Smile":
    Pumpkin.expression='Smile'
elif choices == "Laugh":
    Pumpkin.expression='Laughing face'
else:
    Pumpkin.expression='Scowl'
print("The pumpkin has a", Pumpkin.expression, ".")
time.sleep(4)
print("Now...")
choices=easygui.buttonbox("What do you want the mouth to look like?", choices=["Round, banana-like", "Spiky", "Curly", "Square", "Flaming"])
if choices == "Round, banana-like":
    Pumpkin.mouth='Round'
elif choices == "Spiky":
    Pumpkin.mouth='Spiky'
elif choices == "Curly":
    Pumpkin.mouth='Curly'
elif choices == "Square":
    Pumpkin.mouth='Square'
else:
    Pumpkin.mouth='Flaming'

print("The pumpkin has a", Pumpkin.mouth, "mouth.")
time.sleep(4)
print("Here's your entire combination:")
time.sleep(3)
easygui.msgbox("A pumpkin with", Pumpkin.eyes, "eyes, a", Pumpkin.nose, "nose, and a", Pumpkin.mouth, " ", Pumpkin.expression, ".")
easygui.msgbox("Happy Halloween!")

When I ran the program, I got this:

Time to carve some pumpkins!
First things first...

The pumpkin has Fire eyes.
Okay, let's do the nose.

The pumpin has a Arrowhead nose.
Now, the mouth! But first...

The pumpkin has a Smile .
Now...

The pumpkin has a Round mouth.
Here's your entire combination:
Traceback (most recent call last):
  File "C:/Python33/Pumpkins.py", line 79, in <module>
    easygui.msgbox("A pumpkin with", Pumpkin.eyes, "eyes, a", Pumpkin.nose, "nose, and a", Pumpkin.mouth, " ", Pumpkin.expression, ".")
TypeError: msgbox() takes from 0 to 5 positional arguments but 9 were given

     Uh oh... It seems that you'll always run into problems when you program. This article is getting too long. Looks like Halloween is going to continue tomorrow!
     I challenge you to turn all the button boxes into enter boxes!
     HAPPY HALLOWEEN!

Tuesday, October 29, 2013

A Quick Tip - The Randint of a List

     You'll notice that in our hot dog program, it said this:

print(condimentlist[randomCondiments])

     In this article, we will be focusing on that. So, what the heck did we do here? Well, for starters, the only kind of random function I know so far is randint, so I had to get a little complicated. But, this is the randint of a list. We might be able to see this better if we have a simpler program that focuses mainly on that. Here's a program I wrote a long time ago for an online site, but then I realized it doesn't run Python.

import random
question=0
questionsAsked=0
fortune=0
fortune2=37
theOutput=["Yes.", "Call me AskIt!", "No.", "Maybe.", "Yes. Now go die in a hole.", "Impossible!", "Those who say impossible accomplish nothing.", "Listen to your heart and you will know the right answer.", "No way.", "Stay healthy, pet a squirrel, and wash your hair with blue cheese.", "You need to take a break off of the computer.", "If I had a body, I would strangle you.", "NU WAI!!! TEL MEE MOAR!!!!!!111!!", "NU NORBZ AWR LIEK TOTEZEZ EPIK LIEK UR BEEG TOEZ!!!!1", "I am not sure how to respond to that.", "...", "Without a doubt.", "[ Content Deleted ] JK! Try asking again.", "Just... No.", "Erik Cassel made it.", "Try asking again, and use proper grammar.", "i nu undirstandz ur grammarz.", "I can't hear you! Speak up!", "Ummm... No.", "Hey, you should do that!", "Certainly.", "It is uncertain.", "As Babe Ruth always said, 'Go home and die.'", "It is very unlikely.", "It is very likely.", "NO WAY!!", "Gross!", "Please be appropriate", "No, hackers suck.", "Errrrrrrrrrrrrrr...", "Have a fun time!", "You should not do it", "He who lacks confidence never achieves his goal."]

print("Hello, there!")
while questionsAsked < 9001:
    question=input()
    fortune=random.randint(0, fortune2)
    print(theOutput[fortune])
    theOutput.append(question)
    fortune2=fortune2 + 1
    fortune=random.randint(0, fortune2

     I know, that are some weird lines. I just wrote those to be funny. Unfortunately, I couldn't figure out how to let the user ask unlimited questions, so I made the limit 9,000. Anyway, the variables we'll be focusing on are fortune, fortune2, and theOutput.
     I guess this wasn't the best example, because I made it repeat whatever you type, but let's just go with it. So, fortune's value is 0. And fortune2's value is 37. And theOutput is a big, long list, consisting of around 36 strings. So, in Python, from 0 to the amount of items in the list +1, It is 0 to 37. Either that, or I just made a mistake and fortune2 is supposed to be 36, but I haven't seen a problem with the program yet. So, you know that when you want to print a specific item in a list, you do print(VAR[NUM])? Well, since fortune is a randint function, it counts as an integer, which qualifies as the NUM in the previous statement. So, when you run the program, you get a random item from theOutput each time! And that explains our hot dog program. Anyway, now you can have fun with AskIt!
     I realized something. Do I really need to have while questionsAsked < 9001:? I challenge you to take that line out and see how the program runs!

Sunday, October 13, 2013

Debugging Part 2 - Condiments

     Alright, let's recap: We wrote a program to help study objects, but it ended up a little complicated. We have worked out some problems in the last article, but now we have to face another problem:


mayonnaise
mayonnaise
mayonnaise
Here are the condiments:
[ERROR]

     Let's see how we can fix this... Well, first, we can try doing 2 other variables. randomCooktime2 and randomCooktime3. Let's also add in another list, so now our condiment section looks like this:

condimentlist=["ketchup", "mustard", "relish", "mayonnaise"] #Used for randomCondiments
condimentlist2=["ketchup", "mustard", "relish", "mayonnaise", " "] #Used for randomCondiments 2 and randomCondiments 3
randomCondiments=random.randint(0, 3)
randomCondiments2=random.randint(0, 4)
randomCondiments3=random.randint(0, 4)

     Yes, I used the comments, because finding everything was getting confusing. Now, let's work our magic with the other condiments!

print("Here are the condiments:")
time.sleep(3)
#The first condiment
if randomCondiments == 0:
        print(condimentlist[randomCondiments])
elif randomCondiments == 1:
        print(condimentlist[randomCondiments])
elif randomCondiments == 2:
        print(condimentlist[randomCondiments])
elif randomCondiments == 3:
        print(condimentlist[randomCondiments])

#The second condiment
if randomCondiments2 == 0:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 1:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 2:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 3:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 4:
    print(condimentlist2[randomCondiments2])

#The third condiment
if randomCondiments3 == 0:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 1:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 2:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 3:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 4:
    print(condimentlist2[randomCondiments3])
time.sleep(2)
print("Now our hot dog is complete!")

     Now, run the program. It should do something like 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 5 minutes.
The dog is now a well wiener .
Does it taste better?
Wait, we need condiments!
Let's cook another hot dog...
I have cooked another hot dog for 9 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:
mustard
mustard
mayonnaise
Now our hot dog is complete!

     Well, we got mustard twice. Debugging is obviously very difficult, so we can just say you got extra mustard. But if you want to make it so it never says the same condiment twice, we'll get into things so difficult I might not even be able to state them.

     So, our final program has come out like this:

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"] #Used for randomCondiments
condimentlist2=["ketchup", "mustard", "relish", "mayonnaise", " "] #Used for randomCondiments 2 and randomCondiments 3
randomCondiments=random.randint(0, 3)
randomCondiments2=random.randint(0, 4)
randomCondiments3=random.randint(0, 4)
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...")
randomCookTime2=random.randint(1, 9)
theDog.cook(randomCookTime2)
time.sleep(randomCookTime2)
print("I have cooked another hot dog for", randomCookTime2, "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)
print("Here are the condiments:")
time.sleep(3)
#The first condiment
if randomCondiments == 0:
        print(condimentlist[randomCondiments])
elif randomCondiments == 1:
        print(condimentlist[randomCondiments])
elif randomCondiments == 2:
        print(condimentlist[randomCondiments])
elif randomCondiments == 3:
        print(condimentlist[randomCondiments])

#The second condiment
if randomCondiments2 == 0:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 1:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 2:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 3:
    print(condimentlist2[randomCondiments2])
elif randomCondiments2 == 4:
    print(condimentlist2[randomCondiments2])

#The third condiment
if randomCondiments3 == 0:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 1:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 2:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 3:
    print(condimentlist2[randomCondiments3])
elif randomCondiments3 == 4:
    print(condimentlist2[randomCondiments3])
time.sleep(2)
print("Now our hot dog is complete!")

     I actually think some lines in this program are never used... Oh well.
     That's it for this article. I challenge you to see if the block of code that begins with def __str__(self): really is pointless!
     Also, if you look in that block of code, you will see a function known as len(). What does this one mean?

Friday, October 4, 2013

Debugging

     Alright, let's recap. We wrote a new article called Wieners.py (at least, that's what I named it), but...

I have cooked the wiener for 6 minutes.
The dog is now perfect & pure .

     And then...

I have cooked another hot dog for 6 minutes.
So, that means the hot dog is burnt & black .

     If the dog was cooked for 6 minutes, it shouldn't be burnt & black! That's a bad sign. Well, looks like it's time to do some debugging!
     For the oblivious, a bug is not a real bug in the program. It's a problem with the program. So, to get rid of that bug is called debugging.
     Alright, as I thought about it, I realized that once randomCookTime has chosen a random number, it keeps that number. But why did the status change? Well... I honestly don't know, but I'm guessing that it did choose a different number, it just didn't display it. So, how do we fix this? My first plan is to do randomCookTime=randomCookTime.

print("Wait, we need condiments!")
time.sleep(3)
print("Let's cook another hot dog...")
randomCookTime=randomCookTime
theDog.cook(randomCookTime)
time.sleep(randomCookTime)
print("I have cooked another hot dog for", randomCookTime, "minutes.")

     Will that work? Let's just see:

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 1 minutes.
The dog is now raw & repulsive .
Does it taste better?
Wait, we need condiments!
Let's cook another hot dog...
I have cooked another hot dog for 1 minutes.
So, that means the hot dog is raw & repulsive .
Now, perfect or not, let me put the condiments on it.
Traceback (most recent call last):
  File "C:\Python33\Wieners.py", line 69, in <module>
    if theCondiment==0:
NameError: name 'theCondiment' is not defined

     Oh no! randomCookTime=randomCookTime broke the condiments! But do we give up? No! Never! I didn't expect this kind of error to happen, but I anticipated this to not work. I have a backup plan: randomCookTime2.

print("Wait, we need condiments!")
time.sleep(3)
print("Let's cook another hot dog...")
randomCookTime2=random.randint(1, 9)
theDog.cook(randomCookTime2)
time.sleep(randomCookTime2)
print("I have cooked another hot dog for", randomCookTime2, "minutes.")

     Will this work? I hope so, this is my only backup plan.

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 3 minutes.
The dog is now raw & repulsive .
Does it taste better?
Wait, we need condiments!
Let's cook another hot dog...
I have cooked another hot dog for 7 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:
relish

     Success! But, we still face one problem.
     Only one condiment ends up on the list. What if we want 2, 3, or all the condiments? This is where things get more complicated.
     Plan A is... I got it, we have to do 3 ranges!

print("Now, perfect or not, let me put the condiments on it.")
time.sleep(3)
if randomCondiments == 1:
    for i in range(0, 1):
        print(condimentlist[randomCondiments])
if randomCondiments == 2:
    for i in range(0, 2):
        print(condimentlist[randomCondiments])
if randomCondiments == 3:
    for i in range(0, 3):
        print(condimentlist[randomCondiments])
if randomCondiments == 4:
    for i in range(0, 4):
        print(condimentlist[randomCondiments])
print("Here are the condiments:")

     Will this work? We'll just see.

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 4 minutes.
The dog is now a well wiener .
Does it taste better?
Wait, we need condiments!
Let's cook another hot dog...
I have cooked another hot dog for 9 minutes.
So, that means the hot dog is burnt & black .
Now, perfect or not, let me put the condiments on it.
mayonnaise
mayonnaise
mayonnaise
Here are the condiments:
Traceback (most recent call last):
  File "C:\Python33\Wieners.py", line 81, in <module>
    print(theCondiment)
NameError: name 'theCondiment' is not defined

     That's a bad sign. This article has gone on long enough. We'll have to fix our mayonnaise next time!