简体   繁体   中英

Using input to determine which function to call in Python

I'm supposed to create a function called menu, that will display the menu, check if the input is valid, and if it's not, ask the user to enter the input again. If the input is correct, then it returns the value.

I know that the argument has to be a int, but how do I make it such that it takes in the user's input, and then displays the function accordingly? This is what I have so far.

def menu(choice):
    print("===============================================")
    print("Welcome to Students' Result System")
    print("===============================================")
    print("1: Display modules average scores")
    print("2: Display modules top scorer")
    print("0: Exit")
    choice=int(input("Enter choice:"))
    while (choice=="" or choice!=0 or choice!=1 or choice!=2):
        print("Invalid choice, please enter again")
        choice=int(input("Enter choice:"))
        return choice
    if choice ==1:
        display_modules_average_scores()
    elif choice ==2:
        display_modules_top_scorer()
    elif choice==0:
        print("Goodbye")

def display_modules_average_scores():
    print("average")

def display_modules_top_scorer():
    print ("top")

def main():
    menu(choice)

main()

You have several errors in the code you've presented:

  1. Python is a whitespace-sensitive language. You need to indent your code so that the code relevant to the function is contained at a higher indent level than the def declaration it's associated with.
  2. You have defined choice as an input to the menu function, and are calling menu() using the main() method. However, choice is not a defined variable at that point, and is not defined in your code until you accept the input from the user. Thus, the code will fail when attempting to use this call.

    In the way you have this code currently set up, you can simply remove choice as an input to menu() in both the definition as well as the invocation, and the menu will appear as expected.

  3. You also seem to have a misunderstanding when it comes to creating conditional statements. Your conditionals, joined by an or , will not be evaluated after the first condition that returns true . Thus, a menu choice of 1 will make the conditional choice!=0 TRUE , and will always enter the Invalid choice state.

    If you're interested in validating the user's input, you might want to consider chaining the numeric comparisons (after the blank string checking) using the and keyword:

     while (choice=="" or (choice!=0 and choice!=1 and choice!=2)): 

With all these issues resolved, your code would look more like the following:

def menu():
  print("===============================================")
  print("Welcome to Students' Result System")
  print("===============================================")
  print("1: Display modules average scores")
  print("2: Display modules top scorer")
  print("0: Exit")
  choice=int(input("Enter choice:"))
  while (choice=="" or (choice!=0 and choice!=1 and choice!=2)):
      print("Invalid choice, please enter again")
      choice=int(input("Enter choice:"))
      return choice
  if choice ==1:
      display_modules_average_scores()
  elif choice ==2:
      display_modules_top_scorer()
  elif choice==0:
      print("Goodbye")

def display_modules_average_scores():
    print("average")

def display_modules_top_scorer():
    print ("top")

def main():
    menu()

main()

Repl.it

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