简体   繁体   中英

How do i create a menu using print statements? I want to be able to choose a number and based on what number i choose run that function

def main():

    option=displaymenu()
    print(option)
    while option1 != 4:
        if option1 ==1:
            print("1234")
        elif option1==2:
            print("1")
        elif option1==3:
            print("joe")
        else:
            print("end")



def displaymenu():
    print("choose one of the following options")
    print("1.Calculate x to the power of N")  << this is supposed to be 
    print("2.Calculate factorial of N")
    print("3.Calculate EXP")
    print("4.Exit")
    option1=input("Please enter your choice here:")<<<  i want these print statements to act as a menu? how do I make it so when I input a number 1-4 It does this operation I input ?

I want this to be a input statement that when I input 1 it will do print 1234 as I coded in my main program... help ? why when I enter a 1 or 2 or 3 it does nothing but print "end" ..? help. main()

Your line option=displaymenu() will set option to None as you don't have a return statement at the end of your function. Add the following line to your function and it should work:

return int(option1)

You are not returning back option1 from displaymeny. Instead you are trying to access it in the main function:

 while option1 != 4:

This also means that option in the main function does not have any vaule.

Change your program to this:

def main():
option = -1 # Value to get the while loop started
while option != 4:
    option=displaymenu()
    if option ==1:
        print("1234")
    elif option==2:
        print("1")
    elif option==3:
        print("joe")
    else:
        print("end")



def displaymenu():
    print("choose one of the following options")
    print("1.Calculate x to the power of N")  << this is supposed to be 
    print("2.Calculate factorial of N")
    print("3.Calculate EXP")
    print("4.Exit")
    return input("Please enter your choice here:")<<<  i want these print statements to act as a menu? how do I make it so when I input a number 1-4 It does this operation I input ?

Also note that I moved the call to displaymeny inside the while loop. If you have it outside you will only get the menu once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM