469,636 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Program failing after converting global variables/ to local

I converted this program from using global variables to local variables. When I did that my while loop stopped working in my main function(module). Anyone, any idea why? I underlined the area where I think the problem occured after my changes.

Old Code working FINE
Expand|Select|Wrap|Line Numbers
  1. from math import *
  2.  
  3. def menu():
  4.     global menuSel
  5.     print "\nSlect one of the following:"
  6.     print "1. Calculate Area of Rectangle"
  7.     print "2. Calculate Area of Circle"
  8.     print "3. Quit"
  9.     menuSel=input("Please enter your selection: ")
  10.     while menuSel!=1 and menuSel!=2 and menuSel!=3:
  11.         menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
  12.     return
  13.  
  14. def rectangle():
  15.     print "You have chosen a rectangle."
  16.     b=input("enter base  : ")
  17.     h=input("enter height:")
  18.     print "The area is ", b*h
  19.     return
  20.  
  21. def circle():
  22.     print "You have chosen a circle."
  23.     r=input("enter radius: ")
  24.     print "The area is ",pi*r**2
  25.     return
  26.  
  27.  
  28. def main():
  29.     menu()
  30.     while menuSel!=3:
  31.         if menuSel==1:
  32.             rectangle()
  33.         elif menuSel==2:
  34.             circle()
  35.         menu()
  36.     return
  37.  
  38. main()
  39. raw_input("\nPress enter to exit.")
  40.  
New Code NOT working

Expand|Select|Wrap|Line Numbers
  1. from math import *
  2.  
  3. def menu():
  4.     print "\nSlect one of the following:"
  5.     print "1. Calculate Area of Rectangle"
  6.     print "2. Calculate Area of Circle"
  7.     print "3. Quit"
  8.     menuSel=input("Please enter your selection: ")
  9.     while menuSel!=1 and menuSel!=2 and menuSel!=3:
  10.         menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
  11.     return
  12.  
  13. def rectangle():
  14.     print "You have chosen a rectangle."
  15.     b=input("enter base  : ")
  16.     h=input("enter height:")
  17.     print "The area is ", b*h
  18.     return
  19.  
  20. def circle():
  21.     print "You have chosen a circle."
  22.     r=input("enter radius: ")
  23.     print "The area is ",pi*r**2
  24.     return
  25.  
  26.  
  27. def main():
  28.     choice = menu()
  29.     while choice!=3:
  30.         if choice==1:
  31.             rectangle()
  32.         elif choice==2:
  33.             circle()
  34.         choice =menu()
  35.     return
  36.  
  37. main()
  38. raw_input("\nPress enter to exit.")
  39.  
Dec 6 '06 #1
1 1574
bartonc
6,596 Expert 4TB
You have to return the value from you functions. ie:
Expand|Select|Wrap|Line Numbers
  1. def menu():
  2.     print "\nSlect one of the following:"
  3.     print "1. Calculate Area of Rectangle"
  4.     print "2. Calculate Area of Circle"
  5.     print "3. Quit"
  6.     menuSel=input("Please enter your selection: ")
  7.     while menuSel!=1 and menuSel!=2 and menuSel!=3:
  8.         menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
  9.     return menuSel
In fact, if you don't need value returned (some languages call this a "procedure"), there is no need to put the return at the end. Such functions (and the way you wrote them) return None. Return can also be used to jump out of a fuction at any point. ie:

Expand|Select|Wrap|Line Numbers
  1. if time_to_exit:
  2.     return
Dec 6 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Andrew V. Romero | last post: by
3 posts views Thread by Mahmood Ahmad | last post: by
112 posts views Thread by istillshine | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.