【问题标题】:Python While function [duplicate]Python While函数[重复]
【发布时间】:2021-08-25 10:48:56
【问题描述】:

我在做一个小游戏,由于某种原因这个功能不起作用。

while InstructionsNeeded != 'no' or InstructionsNeeded != 'yes':
        print("please type yes or no")
        InstructionsNeeded = input("Welcome to Dice Brawl. Do you need instructions:")
        InstructionsNeeded = InstructionsNeeded.lower()

我在代码中提供了之前的输入,它工作得非常好。此外,当我查看 InstructionsNeeded 的值时,它会正常读取。

【问题讨论】:

  • InstructionsNeeded 不能同时为“否”和“是”,因此其中一个 or 条件始终为真……想清楚。
  • “不会工作”在什么意义上?例如,您当前缺少 InstructionsNeeded 的初始起始值。
  • 我非常怀疑它是否能正常工作。您的条件始终为真,因为没有字符串可以同时等于 'no''yes'。它应该是 `while ... and ...:
  • 您可能只想将InstructionsNeeded != 'yes' 更改为InstructionsNeeded == 'yes'

标签: python input while-loop conditional-statements


【解决方案1】:

只需将or 运算符更改为and 否则会导致无限循环:

while InstructionsNeeded != 'no' and InstructionsNeeded != 'yes':
        print("please type yes or no")
        InstructionsNeeded = input("Welcome to Dice Brawl. Do you need instructions:")
        InstructionsNeeded = InstructionsNeeded.lower()

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2014-08-29
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多