472,328 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 1696
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

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

Similar topics

10
by: Tony Archer | last post by:
Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a production box. On the dev box...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> ...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses...
1
by: coolindienc | last post by:
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). ...
7
by: kr | last post by:
Hi All, Suppose I consider a sample program as given below:- #include<stdio.h> #include<stdlib.h> int i; int main() { char *test(int i);...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.