Вы можете обернуть свой код через некоторое время l oop, а затем прервать его на основании некоторого условия. Кроме того, вы можете поместить свои функции в диктовку, чтобы сохранить ваше желание написать несколько, если еще logi c.
def apple():
print("This is the apple func")
def orange():
print("this is the orange func")
def mango():
print("This is the mango func")
def banana():
print("this is the banana func")
dict_of_funcs = {
"apple": apple,
"orange": orange,
"mango": mango,
"banana": banana
}
while True:
func_choice = input("Please choose one of the following functions: " + ",".join(dict_of_funcs) + ", or quit to exit: ").lower()
if func_choice in dict_of_funcs:
dict_of_funcs[func_choice]()
elif func_choice == "quit":
break
else:
print("Thats not a valid function")
print("you have finished choosing")
OUTPUT
Please choose one of the following functions: apple,orange,mango,banana, or quit to exit: apple
This is the apple func
Please choose one of the following functions: apple,orange,mango,banana, or quit to exit: mango
This is the mango func
Please choose one of the following functions: apple,orange,mango,banana, or quit to exit: quit
you have finished choosing