Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个python代码哪里出了问题?_Python_Python 3.x - Fatal编程技术网

这个python代码哪里出了问题?

这个python代码哪里出了问题?,python,python-3.x,Python,Python 3.x,我的编程作业一直有问题,我正在尝试添加一个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

我的编程作业一直有问题,我正在尝试添加一个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'

我对python语法的了解非常有限,但我相当确定您需要添加一个中断,以便while循环完成

这似乎很相似,可能会有所帮助!:-)

编辑:正如我所注意到的,我认为上面的答案很准确D

while path = none:
这里要做的是将
none
赋值给
path
,而不是在
path
中检查字符串
'none'

需要更改它来检查变量,而不是设置它:

 while path is 'none':

我可以看到一些问题和可能引起关注的原因。例如,您应该使用
raw\u input()
而不是输入,因为它更安全(其中
input()
调用
eval(raw\u input())
从某种意义上评估输入的文本,这样如果我说我的名字是
1+2
my name with
input()
3
,但带有
raw\u input()
“1+2”
)(请注意,如果您使用的是Python2.x,我相信这与3.x有所不同)

此外,while循环似乎没有任何缩进,作为一项规则,任何冒号后面都应该有一个新的缩进级别


最后,while行还有两个问题,即使用一个=(而不是==)来比较两个值,并且
none
没有被“”包围(没有称为none的变量!)或者,您可以说none,在这种情况下声明
path=none
应该是none(在while循环中也是如此)

您希望使用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语句中使用if不会像您所希望的那样起作用。中缀运算符检查左操作数是否是右操作数集合的元素。您可以尝试以下str方法:

if response.startswith('....')
或按以下方式比较响应的左子字符串

if response[:4] == 'left'

检查它的一种方法是使用
while path!=None

代码: 结果:
但是,公认的方法是使用
,而path不是None
,因为检查对象标识比通过操作数检查相等性更好(这很容易被“欺骗”)。这也符合PEP8的建议。

您的代码有许多错误。请粘贴您所问问题的完整错误消息。这也将有助于解释您是如何尝试解决问题的,因为这里的人通常不太愿意为您做功课,除非您表明您已努力解决问题你自己也有问题。我不知道,我明白,你们不能帮我。这是我最主要的一点:重新编辑代码,因为我认为它是完整的。你将整个代码保留在你的问题上。用缩进正确格式化。如果这不是你最初的缩进,请随意回滚更改。这很有效!谢谢你伙计们!使用
input
并不是天生的错误:记住Python 3.x使用它,而Python 2.x使用
raw\u input
。从OP的代码来看,他使用Python 3.x是非常确定的,因为他的
print
不是一个函数,而是一个语句。在你的回答中也应该注意到这一点。;)啊,是的,很抱歉。。。我还没从2.x石器时代爬出来啊哈哈。你是说input()现在相当于旧的原始输入()?别担心,也没有从2.7的模糊舒适中爬出来。是的,请看。谢谢!我可能应该尽快迁移到3.x,而不是更晚;)正确的方法是在path不是None时使用
如果使用
None
,如PEP8所述:“与None这样的单例进行比较时,应始终使用
is
is not
,永远不要使用相等运算符。”。措辞有点重,但你是对的。不应该使用“适当的”。)
if response[:4] == 'left'
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