Re: [Tutor] When I run this code, it just keeps repeating.

2005-08-01 Thread Bob Gailer


At 06:55 PM 7/30/2005, Nathan Pinno wrote:
...
 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
Note that you give user a 2nd try but that input never reaches the print
statement.
The following might be a bit of a stretch, but consider creating a list
of lists to store the various menu options. 
Doing this separates the high-level menu from lower-level code which is
now not repetitive. To add/modify menu items one just extends/edits the
list. Here's a simple example which ignores some things but
gives you the idea. Warning untested code:
menuItems = [
[Add, First number:, Second
number:, +, lambda(x,y:x+y)],
[Subtract, First number:, Second
number:, -, lambda(x,y:x-y)],
[Exit]]
# first item is the main menu prompt, 
# 2nd and 3rd are the input prompts, 
# 4th is the string representation of the operator, 
# 5th is an anonymous function to do the calculation.
def menu():
 print CALCULATE MENU
 for option, menuItem in enumerate(menuItems):
 print str(option + 1) + ),
menuItem[0]
def cal():
 cal_opt = int(raw_input(Option: ))
 if 1 = cal_opt = len(menuItems):
 menuItem = menuItems[cal_opt - 1]
 if menuItem[0] = Exit:return False
 prompt1, prompt2, oper, func = 
menuItem[1:]
 X = input(prompt1)
 Y = input(prompt2)
 print X, oper, Y, = , func(X, Y)
 else:
 print That's not an option. Try
again.
 return True
print Mini Calculator
print By Nathan Pinno
print
while True:
 menu()
 if not cal(): break

Bob Gailer
phone 510 978 4454 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread Nathan Pinno



When I run the following code it just keeps repeating. Here is a screen 
shot showing what I mean:
Mini CalculatorBy Nathan Pinno

CALCULATE MENU1) Add2) Subraction3) Multiplication4) 
Division w/o remainder5) Division with remaider6) Exponation7) 
Square roots9) ExitOption: 5First number:4Second number:24 / 
2 = 2 R 0First number:3Second number:63 / 6 
= 0 R 3First number:5Second number:65 / 6 = 
0 R 5First number:

Here is the relevant 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(): global cal_opt 
cal_opt = int(raw_input("Option: "))

print "Mini Calculator"print "By Nathan 
Pinno"printmenu()cal()while cal_opt != 9: 
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"

How do I stop this, and make it go back to the main menu?

Thanks in advance,
Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread python-tutor
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
 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: 5
 First number:4
 Second number:2
 4 / 2 =  2  R  0
 First number:3
 Second number:6
 3 / 6 =  0  R  3
 First number:5
 Second number:6
 5 / 6 =  0  R  5
 First number:

 Here is the relevant 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():
 global cal_opt
 cal_opt = int(raw_input(Option: ))

 print Mini Calculator
 print By Nathan Pinno
 print
 menu()
 cal()
 while cal_opt != 9:
 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

 How do I stop this, and make it go back to the main menu?

 Thanks in advance,
 Nathan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When I run this code, it just keeps repeating.

2005-07-30 Thread Nathan Pinno
Thanks Todd. Can you please look at my other messages that you haven't 
answered, and see what's wrong with them, or what the answer is.
- Original Message - 
From: [EMAIL PROTECTED]
To: tutor@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
 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: 5
 First number:4
 Second number:2
 4 / 2 =  2  R  0
 First number:3
 Second number:6
 3 / 6 =  0  R  3
 First number:5
 Second number:6
 5 / 6 =  0  R  5
 First number:

 Here is the relevant 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():
 global cal_opt
 cal_opt = int(raw_input(Option: ))

 print Mini Calculator
 print By Nathan Pinno
 print
 menu()
 cal()
 while cal_opt != 9:
 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

 How do I stop this, and make it go back to the main menu?

 Thanks in advance,
 Nathan
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor