USER_CHOICE ="""Enter one of the following
- 'b' to look for 5-star books
- 'c' to look at the cheapest books
- 'n' to just get the next available book on the catalogue
- 'q' to exit
Enter your choices: """defmenu():
user_input =input(USER_CHOICE)while user_input !='q':if user_input =='b':
print_best_books()elif user_input =='c':
print_cheapest_books()elif user_input =='n':
get_next_book()else:print("Please choose a valid command.")
user_input =input(USER_CHOICE)
menu()
写法 2,这是最好的写法:
USER_CHOICE ="""Enter one of the following
- 'b' to look for 5-star books
- 'c' to look at the cheapest books
- 'n' to just get the next available book on the catalogue
- 'q' to exit
Enter your choices: """# 同上,print_best_books 等都是函数名
user_choices ={'b': print_best_books,'c': print_cheapest_books,'n': get_next_book,}defmenu():
user_input =input(USER_CHOICE)while user_input !='q':if user_input in['b','c','n']:
user_choices[user_input]()else:print("Please choose a valid command.")
user_input =input(USER_CHOICE)
menu()