我定义了一个函数 eval_loop,在其中我用一个命令提示用户,要求他评估一个表达式。我要计算的表达式是“2+2”。如果用户按done或Done,while循环将退出。为什么我输入“2+2”时无法收到任何输出?为什么 eval 命令没有显示任何输出?
def eval_loop ():
while True:
line=input("enter the expression you want to evaluate:")
if line == "done" or "Done":
break
else:
y=eval(line)
print(y)
print (done)
eval_loop()
您有一个错误导致
eval
无法运行。 if line == "done" or "Done":
与 if (line == "done") or "Done"
相同,并且由于 "Done"
始终是 True
,因此您可以在执行任何操作之前跳出循环。最后的打印也是一个错误。
def eval_loop ():
while True:
line=input("enter the expression you want to evaluate:")
if line.lower() == "done":
break
else:
y=eval(line)
print(y)
print ("done")
eval_loop()