473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

making local variable work???? Help

48 New Member
I am not sure what am I supposed to assign for my while loop to work at the bottom. Please help me to understand what I am doing wrong. Below is my code and I have highlighted my main module. I know that is where the problem is.

Andy

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 menuSel
  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 menuSel!=3:
  30.         if menuSel==1:
  31.             rectangle()
  32.         elif menuSel==2:
  33.             circle()
  34.         menu()
  35.     return
  36.  
  37. main()
  38. raw_input("\nPress enter to exit.")
  39.  
Dec 8 '06 #1
7 1278
bartonc
6,596 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     choice = menu()
  3.     while choice != 3:
  4.         if choice == 1:
  5.             rectangle()
  6.         elif choice == 2:
  7.             circle()
  8.         choice = menu()
  9.     return
functions return values not names. so assign whatever name you want to the value of a fuction.
Dec 8 '06 #2
fuffens
38 New Member
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 menuSel
  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.")
I have updated your main function and menu function to do what I think you want to do. If the menu function returns a local variable named choice you must use that one, not the variable named menuSel from the menu function. In the menu function it was a simple tab fault. Only a correct menu selection should be returned.

Best regards
/Fredrik
Dec 8 '06 #3
bvdet
2,851 Recognized Expert Moderator Specialist
Andy,

Here's another (not necessarily the best) way to use 'while' in menu():
Expand|Select|Wrap|Line Numbers
  1.     while True:
  2.         menuSel = input("Please enter your selection (1, 2, or 3): ")
  3.         if menuSel in [1, 2, 3]:
  4.             break
  5.         else:
  6.             print "Invalid Selection"
Unless you need to return a value to the calling function, there is no need to use return:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     while True:
  3.         choice = menu()
  4.         if choice == 1:
  5.             rectangle()
  6.         elif choice == 2:
  7.             circle()
  8.         else:
  9.             break
Function main returns 'None' with or without 'return'.
Dec 9 '06 #4
coolindienc
48 New Member
Fredrik,
That is exactly what I did at first, but as a result I started repeating menu and not actually going for any calculations based on my choice. I will not be able to quit either. I can only go further if I entered wrong choice and I have to enter my choice again from the menu.

Any suggestions why it does so?
Dec 9 '06 #5
bartonc
6,596 Recognized Expert Expert
Fredrik,
That is exactly what I did at first, but as a result I started repeating menu and not actually going for any calculations based on my choice. I will not be able to quit either. I can only go further if I entered wrong choice and I have to enter my choice again from the menu.

Any suggestions why it does so?
look at main(). You weren't assigning a new "choice".
Dec 9 '06 #6
coolindienc
48 New Member
bartonc,
I did so before just like fredrik said even before I posted the discussion and it was not working. OK here is my copy of old one before I posted the earlier one. Below program does not recognize any of my choices. It keeps giving me menu again as a result.

Andy

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 menuSel
  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.  
Dec 9 '06 #7
bartonc
6,596 Recognized Expert Expert
Then I don't know what you problem is. After de-indenting return in menu(), it works for me...


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 menuSel
  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.  
  19. def circle():
  20.     print "You have chosen a circle."
  21.     r=input("enter radius: ")
  22.     print "The area is ",pi*r**2
  23.  
  24.  
  25. def main():
  26.     choice = menu()
  27.     while choice!=3:
  28.         if choice==1:
  29.             rectangle()
  30.         elif choice==2:
  31.             circle()
  32.         choice = menu()
  33.  
  34. main()
Dec 9 '06 #8

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

Similar topics

4
by: Jean-Sébastien Bolduc | last post by:
Hello, I would like to associate a local namespace with a lambda function. To be more specific, here is exactly what I would want: def foo(): a = 1 f = lambda x : a*x return f
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
8
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
3
by: Christine | last post by:
I am working on code that I have posted several questions on, they have been helpful, but as I am new to programming, I have come across something else I am not sure how to fix. This is a build...
3
by: Johannes Zellner | last post by:
Hi, can I make an object read-only, so that x = new_value fails (and x keeps it's orginal value)? This would be especially of interest for me for an object created by a c extension.
28
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " ...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
204
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.