【发布时间】:2020-02-20 19:06:48
【问题描述】:
在给程序一个定义的字符串(例如“add”)之后,这段代码让我问第一个 if input(x) 问题。 输入几次后,它给了我想要的输出。
为什么不在第一次尝试? 另外,当输入为“end”时,如何执行程序结束等循环?
提前谢谢你。 最好的。
x = """Wählen Sie eine der folgenden vorgegebenen Operationen: (add/subtract/multiply/divide/end/history)
"""
a = "Erste Zahl: "
b = "Zweite Zahl: "
ops = ["add", "subtract", "multiply", "divide", "history", "end"]
list = []
if input(x) in ops:
list.append(input(x))
if input(x) == "add":
print(int(input(a)) + int(input(b)))
elif input(x) == "subtract":
print(int(input(a)) - int(input(b)))
elif input(x) == "multiply":
print(int(input(a)) * int(input(b)))
elif input(x) == "divide":
print(int(input(a)) / int(input(b)))
elif input(x) == "history":
print(list)
elif input(x) == "end":
print("Das Programm wird beendet")
else:
print("""Geben Sie bitte eine gültige Eingabe ein""")
【问题讨论】:
-
您反复拨打
input()。将变量命名为list是个糟糕的主意,要小心!
标签: python if-statement input