如何退出python测试

2024-10-03 21:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个基本的数学测验,我必须做,但我还没有想出如何让发挥退出。我已经做了,所以每当你按下-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)

Tags: answerisrandomscoreprintrandintanscorrect
3条回答

请尝试以下代码:

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)

最简单的方法

我刚刚在每个input之后添加了if ans == 1: break

import random

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

print ('Enter -1 to quit')

Score = 0

while True:
    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:
        break
    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 == -1:
        break
    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 == -1:
        break
    if ans == CorrectAnswer3:
        print ('Correct! ')
        Score = Score + 1
    else:
        print ('Incorrect')
        print ('The correct answer is: ', CorrectAnswer3)
        Score = Score - 1

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

顺便说一句,一些提示

如果以后再初始化变量(通过指定新值),则不需要在顶部初始化变量

Python变量通常从小写开始(类从大写开始)

您可以用较短的方式递增和递减x += 1

如果参数之间用空格隔开就好了,例如random.randint(1, 12)

函数后面的空格(如python3中的inputprint)是不必要的

您可以连接新行和字符串,例如print('\nWell done, Your final score is:', Score)顺便说一句,print中的参数默认用空格分隔

对于Python3.6+,可以使用格式字符串,这更简单(请参见f前面的'):
print(f'\nWell done, Your final score is: {Score}')
print(f'What is {num3} + {num4}?')

我使用列表减少了变量的数量。我还向字符串添加了f标志,以避免滥用连接。一般来说,我稍微改进了您的代码(视觉上):)

import random
import sys
import time

print ('Welcome to my quiz. Enter the correct answer for the given math equation.')
print ('Enter -1 to quit \n')

Score = 0
ans = None

num = [random.randint(1,12) for i in range(6)]
num[4] = random.randint(50,100)

CorrectAnswers = [
    num[0] * num[1],
    num[2] + num[3],
    num[4] - num[5]]

while True:


    print (f'What is {num[0]} x {num[1]}?')
    ans = int(input('Answer: '))

    if ans == CorrectAnswers[0]:
        print ('Correct!')
        Score += 1
    elif ans == -1:
        break
    else:
        print (f'Incorrect \n The correct answer is: {CorrectAnswers[0]}')
        Score -= 1



    print (f'What is {num[2]} + {num[3]}?' )
    ans = int (input('Answer: '))

    if ans == CorrectAnswers[1]:
        print ('Correct!')
        Score += 1
    elif ans == -1:
        break
    else:
        print (f'Incorrect \n The correct answer is: {CorrectAnswers[1]}')
        Score -= 1


    print (f'What is {num[4]} - {num[5]}?' )
    ans = int(input('Answer: '))

    if ans == CorrectAnswers[2]:
        print ('Correct!')
        Score += 1
    elif ans == -1:
        break
    else:
        print (f'Incorrect \n The correct answer is:  {CorrectAnswers[2]}')
        Score -= 1


print (f'\n Well done, Your final score is: {Score}')
time.sleep(2)
sys.exit()

关于任务。在Python中有很多方法可以退出,但我仍然选择sys.exit()。 如果您想让这段代码变得完美,请阅读Python中的OOP,并查看Python语言代码风格PEP8

哦:https://realpython.com/python3-object-oriented-programming/

代码样式:https://www.python.org/dev/peps/pep-0008/

相关问题 更多 >