473,698 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program failing after converting global variables/ to local

48 New Member
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 1785
bartonc
6,596 Recognized Expert Expert
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
2556
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 the things a user can do is click a "Change region" link which takes them to a map of the world. They can then select one of four regions. When they select a region it takes them to a page which reads a variable and then sets a cookie which is...
4
24177
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(); function loadUrlVariables() { varString = location.search;
3
2975
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 supposed to be reported on the printer but I have commented those pieces of code and have got those records printed on the screen. I am using Microsoft Visual C++ 6.0 on Microsoft XP (Home) platform. I am facing some problems in getting desire...
59
3917
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 candidate for Python 3000 yet? Chris
18
3129
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 how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate. speakdate just converts a set of integers (date variables) to a string.
1
1452
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 I think the problem occured after my changes. Old Code working FINE from math import * def menu(): global menuSel print "\nSlect one of the following:" print "1. Calculate Area of Rectangle"
7
2115
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
1709
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 a function that is called thousands of times per second, it seems to me that, performance-wise, it would be best to make all variables used in it global, so that memory for them doesn't have to be allocated and released with each call. However,...
112
5435
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 that may print some messages. foo(...) { if (!silent)
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.