如何在“为什么”循环内添加“如果”命令

时间:2018-10-30 13:30:13

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

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

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'

如您所见,在if循环中有一个while条件。但是,当系统提示我输入文本时,应该键入“ help”以激活条件。但是,当我这样做时,while循环将忽略条件。为什么会这样?

1 个答案:

答案 0 :(得分:0)

这是因为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