【问题标题】:why '!= 'and '== 'don't work in while or for loop in python [closed]为什么'!='和'=='在python中的while或for循环中不起作用[关闭]
【发布时间】:2020-09-19 05:35:44
【问题描述】:

没有循环 != 和 == 有效,但如果我将它们放入循环中,它们将不起作用

a = int(input())
while a != 'stop':
x = int(input())
y = str(input())
z = str(input())
if x == 'A':
    x = a
    if y == z:
        a = a + x
    elif y != z:
        a = a - x

【问题讨论】:

  • 什么不完全有效?你输入了什么?期望和观察到的结果是什么?

标签: python python-3.x loops for-loop while-loop


【解决方案1】:

Dude python 关心缩进。您想要通过 while 循环循环的每一行都应该缩进。尝试缩进代码再试一次

【讨论】:

  • 谢谢所有支持这个的人。真的很感激。谢谢
【解决方案2】:

我不是专家,但错误的缩进可能是问题所在。

【讨论】:

  • 目前,您向我们展示了非法的 Python 代码。 while 语句后的行必须缩进更多。另外,为什么您认为您的代码不起作用?它是如何失败的?
【解决方案3】:

修复可能,添加缩进:

a = int(input())
while a != 'stop':
   x = int(input())
   y = str(input())
   z = str(input())
   if x == 'A':
     x = a
     if y == z:
        a = a + x
     elif y != z:
        a = a - x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2018-07-30
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多