【发布时间】:2020-02-14 03:03:55
【问题描述】:
我正在尝试编写提示用户选择功能或退出的代码。我希望它一直提示他们,直到他们输入“退出”或“退出”(任何形式,即全部大写或全部小写)。我似乎无法弄清楚如何让它运行。有什么建议吗?
import math
prompt = '''Enter a number for the function you want to execute.
Type 'exit' or 'quit' to terminate.
1 sin(x)
2 cos(x)
3 tan(x)
4 asin(x)
5 acos(x)
6 atan(x)
7 ln(x)
8 sqrt(x)
9 factorial(x)
:'''
while True:
function = input(prompt)
if function == 'quit' or 'exit':
break
elif function(range(0,10)):
print(f"You entered {function()}!")
else:
print("Answer not valid try again")
functions = {1: math.sin, 2: math.cos, 3: math.tan, 4: math.asin,
5: math.acos, 6: math.atan, 7: math.log, 8: math.sqrt, 9: math.factorial}
【问题讨论】: