[Tutor] Now what do I do?(was Re: When I run this code, it just keeps repeating.)
python-tutor@toddmaynard.com
python-tutor at toddmaynard.com
Sun Jul 31 04:55:42 CEST 2005
You are almost there.....
Do you undestand why you are getting the output that you are getting?
Pretend that you are the computer and walk through what happens when you enter
9 at the menu:
....
while cal_opt != 9:
menu()
cal_opt = cal() # This is where you entered '9' so cal_opt = '9'
if cal_opt == 1: # False so skipped
X = input("First number:" )
Y = input("Second number:" )
print X, "+", Y, "= ",X + Y
<-- code cut out -->
elif cal_opt == 7: # False so skipped to else....
X = input("Number to find the square root of:" )
print "The square root of", X, " = ",X**0.5
else: #.... skipped to here. You never checked for an == '9'
print "That's not an option. Try again."
menu() #menu is printed
cal() # You entered 6, but it isn't saved in any variable
# From here, the loop starts over at the while up top.
# this time the while is false because cal_opt = '9' so
# the computer goes to the first line outside of the loop
print "Goodbye" # which is this.
So the output makes sense, even if it isn't what you intended. There are a
couple of ways to fix this. Can you figure it out on your own?
--Todd
On Saturday 30 July 2005 10:39 pm, Nathan Pinno wrote:
> Here is another screen shot:
> Mini Calculator
> By Nathan Pinno
>
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 3
> First number:4
> Second number:6
> 4 * 6 = 24
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 9
> That's not an option. Try again.
> CALCULATE MENU
> 1) Add
> 2) Subraction
> 3) Multiplication
> 4) Division w/o remainder
> 5) Division with remaider
> 6) Exponation
> 7) Square roots
> 9) Exit
> Option: 6
> Goodbye
>
> And the latest code:
> # This is a small calculator.
> def menu():
> print "CALCULATE MENU"
> print "1) Add"
> print "2) Subraction"
> print "3) Multiplication"
> print "4) Division w/o remainder"
> print "5) Division with remaider"
> print "6) Exponation"
> print "7) Square roots"
> print "9) Exit"
>
> def cal():
> return int(raw_input("Option: "))
>
> print "Mini Calculator"
> print "By Nathan Pinno"
> print
> while cal_opt != 9:
> menu()
> cal_opt = cal()
> if cal_opt == 1:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "+", Y, "= ",X + Y
> elif cal_opt == 2:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "-", Y, "= ",X - Y
> elif cal_opt == 3:
> X = input("First number:" )
> Y = input("Second number:" )
> print X, "*", Y, "= ",X * Y
> elif cal_opt == 4:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y
> elif cal_opt == 5:
> X = input("First number:" )
> Y = input("Second number:" )
> if Y == 0:
> print "Division by zero ot allowed!"
> Y = input("Second number:" )
> else:
> print X, "/", Y, "= ",X / Y," R ", X % Y
> elif cal_opt == 6:
> X = input("First number:" )
> Y = input("Power:" )
> print X, "**", Y, "= ",X**Y
> elif cal_opt == 7:
> X = input("Number to find the square root of:" )
> print "The square root of", X, " = ",X**0.5
> else:
> print "That's not an option. Try again."
> menu()
> cal()
> print "Goodbye"
>
> Thanks in advance,
> Nathan Pinno.
> ----- Original Message -----
> From: <python-tutor at toddmaynard.com>
> To: <tutor at python.org>
> Sent: Saturday, July 30, 2005 8:10 PM
> Subject: Re: [Tutor] When I run this code, it just keeps repeating.
>
> > Looks like you are making some pretty good progress.
> >
> > The short answer to your question is that the menu & user input need to
> > be inside the while loop. That way cal_opt has a chance to change value
> > before
> > it gets evaluated by the while loop again.
> >
> > Another comment - the global cal_opt is considered evil by many. Better
> > would
> > be:
> >
> > def cal():
> > return int(raw_input("Option: "))
> >
> > print "Mini Calculator"
> > print "By Nathan Pinno"
> > print
> >
> > while cal_opt != 9:
> > menu()
> > cal_opt=cal()
> >
> > if cal_opt == 1:
> > X = input("First number:" )
> > Y = input("Second number:" )
> > print X, "+", Y, "= ",X + Y
> >
> > ....
> >
> >
> > Keep hacking away at it... you're making good progress.
> >
> > --Todd
> >
> > On Saturday 30 July 2005 09:55 pm, Nathan Pinno wrote:
> >> When I run the following code it just keeps repeating. Here is a screen
> >> shot showing what I mean: Mini Calculator
>
> cut screen shot and code
>
> >> Thanks in advance,
> >> Nathan
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list