Python原始输入()语法错误
每当我运行这段代码时,它总是指向Python原始输入()语法错误,python,Python,每当我运行这段代码时,它总是指向raw\u input()中的“t”并说SyntaxError:invalid syntax.我做错什么了吗 if key == True: when raw_input().lower() = "get": print "Sorry, there is nothing more to get here." 正确的语法是 if key == True: if raw_input().lowe
raw\u input()
中的“t”并说SyntaxError:invalid syntax.
我做错什么了吗
if key == True:
when raw_input().lower() = "get":
print "Sorry, there is nothing more to get here."
正确的语法是
if key == True:
if raw_input().lower() == "get":
print "Sorry, there is nothing more to get here."
当时没有关键字,我知道你的意思,但你在写某种伪代码。试试这个:
if key:
while True:
reply = raw_input().lower()
if reply == "get":
print "Sorry, there is nothing more to get here."
break
# else ...
我省去了==True
,这只是个人喜好,如果你喜欢,使用它也没什么错
我将用户输入的返回字符串存储在
reply
中,因为您可能正在处理用户的回复。我还把它放在一个循环中,你可以在when
而不是if
时使用这个词来暗示这一点<代码>中断
立即退出循环。当时没有关键字;您想要如果。另外,print
的缩进是关闭的,它必须相对于if
(现在是“when”)缩进。当是一个实验性的Perl关键字而不是Python时,它应该是“==”而不是“=”。@cdarke:当我一开始读你的评论时,我以为你的意思是“没有关键字if key:
”。我终于明白你的意思了。@RoryDaulton:哦,对不起。@cdarke:没必要道歉。我是在指出我的弱点,不是你的。