开发者

Python go to def

开发者 https://www.devze.com 2023-01-26 21:50 出处:网络
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

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 me开发者_开发问答nu. 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.

sum is a built-in function in Python. So you cannot name your function sum. You have to call yourself something else, like sum_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.

def f():
        y()
f()  
def y():
        print 'Y called'

Results in this error:

NameError: global name 'y' is not defined

Because at your program runs, it is not aware of y because the y has not been declared at that point, since the program jumps to f().

To fix this, you would do:

def f():
        y()    
def y():
        print 'Y called'            
f() 

Also, call the function as func_name() and not return func. And since in your sum and subtraction, you are returning the values, save them in some variable and print them, or print them directly.

def sum():
    return 8 + 4

No output

def sum():
    print 8 + 4


sum is a builtin at the point you're calling menu(). If you move this call after defining sum and substraction, it won't give you this error.


Please place the sum and subtraction functions above the menu() function definition. You are calling into the built-in sum(iterable[, start]) function provided by python.


the error is because you're using the sum function before you declare it. it should raise a NameError, but the sum function is a builtin so you're calling that func ( that requires at least one argument) and not the func you've written...

to pass through you can call menu() after declaring the sum and subtraction func

ps it's not a good idea to overwrite python's builtin function..change name to your func, and call menu later, or you will get a NameError


You should wrap the call to menu() in a if __name__ == '__main__:' block. At the bottom of your code, just add

if __name__ == '__main__':
    menu()

This will help prevent using variables before they're defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消