Hi there
First of all just let me say that I’m new in Python and this is for a school work so this should be done without advanced programing and global functions. Using Python 2.6.6 and WingIDE 101
I need a program to present the user with a menu. The user must pick an option and accordingly to is pick the program does what the user wants.
For example, in the code bellow (its not the actual code), if the user picks 1 it goes to the sum() function.
def menu():
print "What do you want? "
print " 1 for sum"
print " 2 for subtraction"
pick = raw_input("please insert 1 or 2 ")
if pick == "1":
return sum()
if pick == "2":
return subtraction()
else:
menu()
menu()
def sum():
return 8 + 4
def subtraction():
return 8 - 4
I what to know how do I send, after my pick, the program to execute an determined definition.
Thanks
P.S. – running this gives me this error:
Traceback (most recent call last):
File “/usr/lib/wingide-101-3.2/src/debug/tserver/_sandbox.py”, line 12, in
File “/usr/lib/wingide-101-3.2/src/debug/tserver/_sandbox.py”, line 7, in menu
TypeError: sum expected at least 1 arguments, got 0
There are lots of things wrong with this, so we will take up one by one.
sumis a built-in function in Python. So you cannot name your functionsum. You have to call yourself something else, likesum_foo. Or_sum.Also, your code is executed from top to bottom. So if you are calling a function say X in a function like Y.
Results in this error:
Because at your program runs, it is not aware of
ybecause theyhas not been declared at that point, since the program jumps tof().To fix this, you would do:
Also, call the function as
func_name()and notreturn func. And since in yoursumandsubtraction, you are returning the values, save them in some variable and print them, or print them directly.No output