【问题标题】:Why is the while loop with the upper case Right is not running?为什么大写 Right 的 while 循环没有运行?
【发布时间】:2020-02-07 12:23:41
【问题描述】:

下面是我的代码:

    name = input ('Whats ur name ? ')

    dtion = input (name + ' you are in the forest where do u wanna go? ')

    while dtion != "right" or dtion != "Right":

       dtion = input ("you are still in the Forest where do u wanna go? ")  

    print ("You are out of the Forest")

【问题讨论】:

  • 请修正你的缩进。
  • dtion != "right" or dtion != "Right" 始终为True,因为dtion 不能同时是两个不同的字符串。

标签: python-3.x boolean-operations or-operator


【解决方案1】:

您编写的条件永远不会变为假,因为对于其中一个条件,dtion 始终为真,因此整体条件始终为真。

你要找的条件是这样的:

while not (dtion == "right" or dtion == "Right"):

它将像这样工作:

【讨论】:

  • 感谢@Waqas,我仍然不清楚它背后的规则,但它现在运行完美。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 2019-09-16
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-03-13
相关资源
最近更新 更多