Author Topic: What's wrong with this Python program?  (Read 1194 times)

Hi, I was trying to write a Python area calculator that repeats, and then ends when you want it to. This is the code:
Code: [Select]
keep_going = True
while(keep_going == True):

    print " "
    print "----------------------------"
    print "Hello, I'm a simple calculator coded by Max Farr. I can tell you the area of a square, a rectangle, a triangle, or a circle."
    print "Which would you like to see?"
    print "----------------------------"
    print
   
    print "1 Square"
    print "2 Rectangle"
    print "3 Triangle"
    print "4 Circle"
   
    shape = input ("> ")
   
    if shape == 1:
        lengthwidth = input ("What is the length of a side of the square?")
        area = lengthwidth*2
        print "The area is", area
   
    if shape == 2:
        length = input ("What is the length of the rectangle?")
        width = input ("What is the width of the rectangle?")
        area = length*width
        print "The area is", area
   
    if shape == 3:
        base = input ("What is the length of the base of the triangle?")
        height = input ("What is the height of the triangle?")
        area = base*height/2
        print "The area is", area
   
    else:
        radius = input ("What is the radius of the circle?")
        area = radius*radius*3.14159265358
        print "The area is approximately", area

    print "Alright!"   
    goagain = input("Do you want to run again? answer yes or no")
       
    if goagain == yes:
        keep_going = True
        print "Cool!"
   
    else:
        keep_going = False
        print "Goodbye!"
When I run it, and it comes to the part where it asks you if you want to go again, and I answer yes or no, it gives me this:
Code: [Select]
Traceback (most recent call last):
  File "C:/Python27/[the file name]", line 41, in <module>
    goagain = input("Do you want to run again? answer yes or no")
  File "<string>", line 1, in <module>
NameError: name 'no [b](or yes, if I answer yes)[/b]' is not defined

Also, when I calculate the area of a square or rectangle, it then asks me what "the radius of the circle is". Then it calculates the circle. o_o Basically, it goes into the circle calculator. o_o

Can you guys help me with these two issues?

Bump - for great justice!

Use raw_input . input() is only for numbers and expressions, but should NEVAR be used, as it's insecure.

Also, try
Code: [Select]
import math
at the top, and then
Code: [Select]
math.pi
whenever you need to use the value of pi. It has more digits and is more elegant.

Use raw_input . input() is only for numbers and expressions, but should NEVAR be used, as it's insecure.
I'm new to this. :U
Taking just the calculator part, how do I make it loop?
Also, how do I fix the square and rectangle - circle problem?

For the looping thing, do I tell it to go to a certain line or something?

I'm new to this. :U
Taking just the calculator part, how do I make it loop?
Also, how do I fix the square and rectangle - circle problem?
What rectangle-circle problem?

To make it loop forevar:
enclose the whole thing in
Code: [Select]
while True:
     ##the bit that needs to repeat goes here, indented
and to break out of the loop (for example if the user says to stop), just put "break"
Also, don't use multiple ifs, instead use
Code: [Select]
if foo:
     something
elif bar:
     something else
else:
     something completely different
That way, if the first one's true, it doesn't go into the other ones, and also:
if you use multiple ifs and then an "else", the else only applies to the last "if", so even if "foo" is true in the example I gave, then both "something" and "something completely different" will run. Is this  the circle-square-triangle problem you mentioned?

also, you SHOULD NEVER USE INPUT()
ONLY use raw_input()
if you need to get a number, do int(raw_input()) (if you need to get just an integer) or float(raw_input()) (if you need decimal points)

also, you SHOULD NEVER USE INPUT()
ONLY use raw_input()
if you need to get a number, do int(raw_input()) (if you need to get just an integer) or float(raw_input()) (if you need decimal points)
What the hell :U
Could you rewrite my code so it fixes the two problems?


What rectangle-circle problem?

To make it loop forevar:
enclose the whole thing in
Code: [Select]
while True:
     ##the bit that needs to repeat goes here, indented
and to break out of the loop (for example if the user says to stop), just put "break"
But how do I give the option for the user to repeat the program or stop it after a calculation runs?
Also yes, the elif thing fixed the one problem.

Code: [Select]
import math

choices = set(['square', 'rect', 'circle'])

def print_choices():
print "I can measure the area of lots of shapes! Which shape should I measure?"
print "Type ",
for choice in choices:
print choice,
print


print "Hello, I'm a magical bean dancing cat."

while True:
print_choices()
shape_raw = raw_input("Which shape would you like? ")
shape = shape_raw.lower().strip() ##Lower makes it lowercase, strip turns, say " rect" into "rect"
if shape not in choices:
print "Uhh, that's not an option."
continue ##restarts the loop from the beginning

if shape == "square":
length = int(raw_input("What is the length of a side? "))
        width = length
        area = length*width
elif shape == "rect":
length = int(raw_input("What is the length of the rectangle? "))
                width = int(raw_input("What is the width of the rectangle? "))                                         
                area = length*width
elif shape == "circle":
r = int(raw_input("What is the radius of the circle? "))
                area = (r**2)*math.pi
print area
answer = raw_input("Should I continue [y/n]:")
if answer == "n":
break ## <------------------- LOOK HERE

You're using a bunch of commands I haven't learned yet :C


Code: [Select]
choices = [1,2,3,4,5]
for choice in choices:
     if choice is 1:
          continue
     elif choice is 3:
          break
     print choice

result = 1 in choices
print result
result = 4 in choices
print result
result = 9001 in choices
print result
Try that. It should be self-explanatory.
« Last Edit: September 28, 2010, 12:36:00 AM by cuddlefish »