提问人: 提问时间:5/3/2021 最后编辑:James Z 更新时间:5/3/2021 访问量:29
我已经声明了'started'=False,在嵌套的if条件中,打印的结果与我预期的相反
I've declared 'started'=False where in the nested if condition the printed results are inversed to what I expected
问:
started=False
while True:
command=input("Enter your command: ").lower()
if command=='start':
if started:
print("Car is has successfully started. Engines on!")
else:
started=True
print("Car has already started!")
else:
print("Enter correct command")
在这里,我向您介绍了我卡住的程序部分。假设在嵌套循环中,如果变量 'started' 是 false(因为它被设置),它应该打印第一个 print 语句,但它没有。它以某种方式打印 else print 语句。基本上,结果反转为我想要的。我的布尔逻辑错了。请详细解释为什么会发生这种情况。
答: 暂无答案
评论
if
True
started
not
if not started:
print
if started:
"Car has already started!"
print