我的编程作业一直存在问题,我正在尝试添加一个while语句,所以如果用户键入代码不理解的响应,它会再次尝试,当我编译代码时坚持第4和第5行,请帮助!
print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path = none:
print('You walk down a corridor, do you go left or right?')
response = input()
if 'left' in response:
print('You take the left turn.')
path = 'left'
elif 'right' in response:
print('You take the right turn.')
path = 'right'
else:
print('Sorry, I didnt understand')
path = 'none'
答案 0 :(得分:1)
while path = none:
您在此处执行的操作是将none
分配给path
,而不是检查'none'
中的字符串path
。
需要更改以检查变量而不是设置它:
while path is 'none':
答案 1 :(得分:1)
print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = None
while path is None:
print('You walk down a corridor, do you go left or right?')
response = input()
if 'left' in response:
print('You take the left turn.')
path = 'left'
elif 'right' in response:
print('You take the right turn.')
path = 'right'
else:
print('Sorry, I didnt understand')
答案 2 :(得分:1)
您希望使用 while 语句循环的代码块必须缩进,即
print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path = none:
print('You walk down a corridor, do you go left or right?')
response = input()
if 'left' in response:
print('You take the left turn.')
path = 'left'
elif 'right' in response:
print('You take the right turn.')
path = 'right'
else:
print('Sorry, I didnt understand')
path = 'none'
其次,您在while循环条件中将赋值( = )与相等( == )混为一谈。它应该是:
while path == 'none':
...
此外,我认为if语句中的 中的将无法按预期运行。 in 中缀运算符检查左操作数是否是右操作数集合的元素。您可以尝试以下 str 方法:
if response.startswith('....')
或比较响应的左子串如下
if response[:4] == 'left'
答案 3 :(得分:0)
我对python语法的了解非常有限,但我很确定你需要添加一个break,以便你的while循环结束。
Emulate a do-while loop in Python?
这看起来很相似,可能有所帮助! : - )
编辑:我认为上面的答案就像我注意到的那样钉了它。 :d
答案 4 :(得分:0)
我可以看到一些问题和可能引起关注的原因。例如,您应该使用raw_input()
而不是输入,因为它更安全(在某种意义上input()
调用eval(raw_input())
,评估输入的文本,这样如果我说我的名字是{ {1}}我的1+2
名称为input()
,但3
为raw_input()
)(请注意,如果您使用的是Python 2.x,我相信它是不同的3.X)
另外,你的while循环似乎没有任何缩进,因为任何冒号都应该跟着一个新的缩进级别。
最后你的while行还有一些问题,即你使用单个=(而不是==)来比较两个值,并且"1+2"
没有被''包围(没有变量)不管怎么说!)或者,你可能想说None,在这种情况下,声明none
应该是None(和while循环中相同)
答案 5 :(得分:0)
检查它的一种方法是使用while path != None
。
print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path != None:
print('You walk down a corridor, do you go left or right?')
response = input()
if 'left' in response:
print('You take the left turn.')
path = 'left'
elif 'right' in response:
print('You take the right turn.')
path = 'right'
else:
print('Sorry, I didnt understand')
path = None
>> nanashi@ubuntu:~/Documents/Python 2.7/Scrapers$ python test9.py
>> Enter your name...
>> Nanashi
>> Welcome to the game, Adventurer Nanashi!
>> You walk down a corridor, do you go left or right?
>> ehhh, what's up, doc?
>> Sorry, I didnt understand
>> nanashi@ubuntu:~/Documents/Python 2.7/Scrapers$ python test9.py
>> Enter your name...
>> Nanashi
>> Welcome to the game, Adventurer Nanashi!
>> You walk down a corridor, do you go left or right?
>> left
>> You take the left turn.
>> You walk down a corridor, do you go left or right?
>> right
>> You take the right turn.
>> You walk down a corridor, do you go left or right?
>> eeeeh, what's up doc?
>> Sorry, I didnt understand
但是,接受这样做的方法是使用while path is not None
,因为检查对象标识比通过操作数检查相等性更好(这很容易就是"欺骗&#34)。这也符合PEP8的建议。