我正在尝试使用python创建简单的测验。当用户键入无效字符时出现问题。有没有办法让我回想起这个问题?代码:
count = 1
q1 = raw_input("Question test")
if q1 == "1":
print ("Correct")
count = count + 1
elif q1 == "2":
print ("Sorry, incorrect answer.")
elif q1 == "3":
print ("Sorry, incorrect answer.")
elif q1 == "4":
print ("Sorry, incorrect answer.")
else:
print ("Sorry, invalid input, try again.")发布于 2014-01-29 03:47:20
count = 1
q1 = raw_input("Question test")
sucess = False
while not success:
if q1 == "1":
success = True
...
else:
print ("Sorry, invalid input, try again.")
print("Success ! :) ")您可以在需要时递增计数。我不知道你到底想要什么。
发布于 2014-01-29 03:48:03
您可能应该将该代码放入一个循环中。类似于"while answer is invalid invalid for input ask“之类的话。
发布于 2014-01-29 03:53:07
使用循环来重复这个问题,直到你离开它,这里我让循环检查1的值,当答案正确/不正确时,它将它设置为0,中断它,并继续执行脚本,如果他们输入外部输入,它会再次尝试问题,因为z仍然等于1。
z = 1
count = 1
while z = 1:
q1 = raw_input("Question test")
if q1 == "1":
print ("Correct")
count = count + 1
z = 0
elif q1 == "2":
print ("Sorry, incorrect answer.")
z = 0
elif q1 == "3":
print ("Sorry, incorrect answer.")
z = 0
elif q1 == "4":
print ("Sorry, incorrect answer.")
z = 0
else:
print ("Sorry, invalid input, try again.")https://stackoverflow.com/questions/21415288
复制相似问题