首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Python中的raw_ input / input

调用Python中的raw_ input / input
EN

Stack Overflow用户
提问于 2014-01-29 03:44:51
回答 3查看 1.2K关注 0票数 0

我正在尝试使用python创建简单的测验。当用户键入无效字符时出现问题。有没有办法让我回想起这个问题?代码:

代码语言:javascript
复制
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.")
EN

回答 3

Stack Overflow用户

发布于 2014-01-29 03:47:20

代码语言:javascript
复制
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 ! :) ")

您可以在需要时递增计数。我不知道你到底想要什么。

票数 0
EN

Stack Overflow用户

发布于 2014-01-29 03:48:03

您可能应该将该代码放入一个循环中。类似于"while answer is invalid invalid for input ask“之类的话。

票数 0
EN

Stack Overflow用户

发布于 2014-01-29 03:53:07

使用循环来重复这个问题,直到你离开它,这里我让循环检查1的值,当答案正确/不正确时,它将它设置为0,中断它,并继续执行脚本,如果他们输入外部输入,它会再次尝试问题,因为z仍然等于1。

代码语言:javascript
复制
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.")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21415288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档