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 -
from math import *
-
-
def menu():
-
print "\nSlect one of the following:"
-
print "1. Calculate Area of Rectangle"
-
print "2. Calculate Area of Circle"
-
print "3. Quit"
-
menuSel=input("Please enter your selection: ")
-
while menuSel!=1 and menuSel!=2 and menuSel!=3:
-
menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
-
return menuSel
-
-
def rectangle():
-
print "You have chosen a rectangle."
-
b=input("enter base : ")
-
h=input("enter height:")
-
print "The area is ", b*h
-
return
-
-
def circle():
-
print "You have chosen a circle."
-
r=input("enter radius: ")
-
print "The area is ",pi*r**2
-
return
-
-
- def main():
-
choice = menu()
-
while menuSel!=3:
-
if menuSel==1:
-
rectangle()
-
elif menuSel==2:
-
circle()
-
menu()
-
return
-
-
main()
-
raw_input("\nPress enter to exit.")
-
7 1278 bartonc 6,596
Recognized Expert Expert - def main():
-
choice = menu()
-
while choice != 3:
-
if choice == 1:
-
rectangle()
-
elif choice == 2:
-
circle()
-
choice = menu()
-
return
functions return values not names. so assign whatever name you want to the value of a fuction.
- from math import *
-
-
def menu():
-
print "\nSlect one of the following:"
-
print "1. Calculate Area of Rectangle"
-
print "2. Calculate Area of Circle"
-
print "3. Quit"
-
menuSel=input("Please enter your selection: ")
-
while menuSel!=1 and menuSel!=2 and menuSel!=3:
-
menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
-
return menuSel
-
-
def rectangle():
-
print "You have chosen a rectangle."
-
b=input("enter base : ")
-
h=input("enter height:")
-
print "The area is ", b*h
-
return
-
-
def circle():
-
print "You have chosen a circle."
-
r=input("enter radius: ")
-
print "The area is ",pi*r**2
-
return
-
-
-
def main():
-
choice = menu()
-
while choice!=3:
-
if choice==1:
-
rectangle()
-
elif choice==2:
-
circle()
-
choice = menu()
-
return
-
-
main()
-
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
bvdet 2,851
Recognized Expert Moderator Specialist
Andy,
Here's another (not necessarily the best) way to use 'while' in menu(): - while True:
-
menuSel = input("Please enter your selection (1, 2, or 3): ")
-
if menuSel in [1, 2, 3]:
-
break
-
else:
-
print "Invalid Selection"
Unless you need to return a value to the calling function, there is no need to use return: - def main():
-
while True:
-
choice = menu()
-
if choice == 1:
-
rectangle()
-
elif choice == 2:
-
circle()
-
else:
-
break
Function main returns 'None' with or without 'return'.
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?
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".
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 -
from math import *
-
-
def menu():
-
print "\nSlect one of the following:"
-
print "1. Calculate Area of Rectangle"
-
print "2. Calculate Area of Circle"
-
print "3. Quit"
-
menuSel=input("Please enter your selection: ")
-
while menuSel!=1 and menuSel!=2 and menuSel!=3:
-
menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
-
return menuSel
-
-
def rectangle():
-
print "You have chosen a rectangle."
-
b=input("enter base : ")
-
h=input("enter height:")
-
print "The area is ", b*h
-
return
-
-
def circle():
-
print "You have chosen a circle."
-
r=input("enter radius: ")
-
print "The area is ",pi*r**2
-
return
-
-
-
def main():
-
choice = menu()
-
while choice!=3:
-
if choice==1:
-
rectangle()
-
elif choice==2:
-
circle()
-
choice = menu()
-
return
-
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... - from math import *
-
-
def menu():
-
print "\nSlect one of the following:"
-
print "1. Calculate Area of Rectangle"
-
print "2. Calculate Area of Circle"
-
print "3. Quit"
-
menuSel=input("Please enter your selection: ")
-
while menuSel!=1 and menuSel!=2 and menuSel!=3:
-
menuSel=input("Please re-enter a valid selection of 1, 2, or 3: ")
-
return menuSel
-
-
def rectangle():
-
print "You have chosen a rectangle."
-
b=input("enter base : ")
-
h=input("enter height:")
-
print "The area is ", b*h
-
-
def circle():
-
print "You have chosen a circle."
-
r=input("enter radius: ")
-
print "The area is ",pi*r**2
-
-
-
def main():
-
choice = menu()
-
while choice!=3:
-
if choice==1:
-
rectangle()
-
elif choice==2:
-
circle()
-
choice = menu()
-
-
main()
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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...
|
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.
| |
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' "
...
|
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...
|
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...
|
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();...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |