【问题标题】:How do I add 'if' commands inside a 'why' loop [closed]如何在“为什么”循环中添加“if”命令[关闭]
【发布时间】:2019-04-03 13:29:48
【问题描述】:

所以我有一些代码,像这样:

while loopCode == 1:
userCommand = raw_input('Type a command (type \'help\' for syntax): ')

if userCommand == help:
    print 'flight: this command will be used to list a flight'
    print 'restaurant: you will be prompted to type the name of the restaurant, and how much you will spend there'

如您所见,while 循环内有一个if 条件。但是当我被提示输入文本时,我应该输入“帮助”来激活条件。但是当我这样做时,while 循环会忽略条件。这是为什么呢?

【问题讨论】:

  • 确保您问题中的代码与您机器上的代码完全相同(包括缩进等)
  • 您的代码引用了一个名为 help 的变量,您尚未在代码中定义该变量。
  • 没有办法退出那个循环...
  • 谢谢@khelwood,我必须确保我声明它是一个字符串。

标签: python if-statement while-loop raw-input


【解决方案1】:

这是因为if语句没有正确缩进,并且它的条件没有指定为字符串'help':

while  1:
    userCommand = input('Type a command (type \'help\' for syntax): ')
    if userCommand == 'help':
        print ('flight: this command will be used to list a flight')
        break

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多