473,385 Members | 1,292 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1774
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 everything works fine, in production it hoses: One of...
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"> var variablesInUrl; var vArray = new Array(); ...
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 into a binary file. The invalid records are...
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 thought of this and suggested it? Is this a...
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 me about the program (even though it works) is...
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). Anyone, any idea why? I underlined the area where...
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); char *tmp = NULL;
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 actual issue. Still I'd like to know... If I have...
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 silent = 1 in my main.c. I have many functions...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.