python - 如何退出 Python 测验

标签 python python-requests

这是我必须做的基本数学测验,但我还没有弄清楚如何让游戏退出。我已经做到了,只要你按-1,你就会退出。它有效,但仅对每三个问题提出一次,所以我希望改变它。

我只是一个刚开始学习Python的学生,所以请给出任何建议并解释我做错了什么。如果您有任何改进的想法,那么我请求您尽可能分享。

import random

print ('Welcome to my quiz. Enter the correct answer for the given math equation.')

print ('Enter -1 to quit')

Score = 0
UserInput = 0
CorrectAnswer = 0
CorrectAnswer2 = 0
CorrectAnswer3 = 0
IncorrectAnswer = 0
num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
num6 = 0
ans = 0

while ans != -1:
    print ('\n')
    num1 = random.randint (1,12)
    num2 = random.randint (1,12)
    num3 = random.randint (1,25)
    num4 = random.randint (1,25)
    num5 = random.randint (50,100)
    num6 = random.randint (1,25)
    CorrectAnswer = num1 * num2
    CorrectAnswer2 = num3 + num4
    CorrectAnswer3 = num5 - num6



    print ('What is ', num1, ' x ', num2, '?')
    ans = int (input ('Answer: '))
    if ans == CorrectAnswer:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer)
        Score = Score -1

    print ('What is ', num3, ' + ', num4, '?' )
    ans = int (input ('Answer: '))
    if ans == CorrectAnswer2:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer2)
        Score = Score -1

    print ('What is ', num5, ' - ', num6, '?' )
    ans = int (input ('Answer: '))
    if ans == CorrectAnswer3:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer3)
        Score = Score - 1

    if ans == -1:



print ('\n')
print ('Well done, Your final score is: ', Score)

最佳答案

试试这个代码:

import random

print ('Welcome to my quiz. Enter the correct answer for the given math equation.')

print ('Enter -1 to quit')

Score = 0
UserInput = 0
CorrectAnswer = 0
CorrectAnswer2 = 0
CorrectAnswer3 = 0
IncorrectAnswer = 0
num1 = 0
num2 = 0
num3 = 0
num4 = 0
num5 = 0
num6 = 0
ans = 0


print('\n')
num1 = random.randint (1,12)
num2 = random.randint (1,12)
num3 = random.randint (1,25)
num4 = random.randint (1,25)
num5 = random.randint (50,100)
num6 = random.randint (1,25)
CorrectAnswer = num1 * num2
CorrectAnswer2 = num3 + num4
CorrectAnswer3 = num5 - num6


print ('What is ', num1, ' x ', num2, '?')
ans = int (input ('Answer: '))
if ans != -1:
    if ans == CorrectAnswer:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer)
        Score = Score -1

    print ('What is ', num3, ' + ', num4, '?' )
    ans = int (input ('Answer: '))
    if ans == CorrectAnswer2:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer2)
        Score = Score -1

    print ('What is ', num5, ' - ', num6, '?' )
    ans = int (input ('Answer: '))
    if ans == CorrectAnswer3:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer3)
        Score = Score - 1
else:
    pass


print('\n')
print ('Well done, Your final score is: ', Score)

关于python - 如何退出 Python 测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58913904/

相关文章:

Python写二进制

python - 是否只有一个 Python 解释器执行多个并发脚本?

python - 如何从重定向的 URL 下载文件?

python - 请求库 https 通过代理获取导致错误

python - Fiddler 请求到 Python Requests 请求

python - DoesNotExist 在/accounts/register/站点匹配查询不存在。 ( Django , python )

android - 在 Ubuntu 上配置 apache ant 时出错

python - 使用 simple-salesforce python 上传多个文件

python - 使用 Python 通过 POST 多部分表单数据上传文件

angularjs - Python flask 和AngularJS与链接http post请求顺序