【发布时间】:2014-03-05 15:46:06
【问题描述】:
我尝试了几种方法来修复此循环,但它无法正常工作。截至目前,它给了我一个语法错误,在第一个打印语句中的 yes 之后突出显示引号......我看不出有什么问题吗?
Ycount = 0
Ncount = 0
Valid = ["Y","y"]
InValid = ["N","n"]
Quit = ["Q","q"]
Uinp = ""
while Uinp != "Q":
Uinp = raw_input("Did you answer Y or N to the question(enter Q to quit)? ")
if Uinp in Valid:
Ycount = Ycount + 1
print "You have answered yes" ,Ycount, "times"
print "You have answered no" ,Ncount, "times"
elif Uinp in InValid:
Ncount = Ncount + 1
print "You have answered yes" ,Ycount, "times"
print "You have answered no" ,Ncount, "times"
elif Uinp in Quit:
break
【问题讨论】:
-
显然它也很有趣地复制了......变量在我评论的末尾......
-
你的代码适合我。
-
什么是语法错误 - 编辑器中的突出显示,或者当您尝试运行它时的解释器?...您使用的是什么版本的 Python:它是 2.x 还是3.x ?
-
我在该代码中根本看不到语法错误。如果这是 Python 3.x,您只会收到语法错误。使用
2to3将其转换为 Python 3 兼容代码。 -
我会推荐你关注Python naming conventions
标签: python syntax while-loop