首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python测验中退出

如何在python测验中退出
EN

Stack Overflow用户
提问于 2019-11-18 19:48:30
回答 3查看 74关注 0票数 1

这是一个基本的数学测验,我必须做,但我还没有弄清楚如何让游戏退出。

我只是一个初学python的学生,所以请给我任何建议,并解释我做错了什么。如果你有任何改进的想法,那么如果可能的话,我请求你分享它。

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

回答 3

Stack Overflow用户

发布于 2019-11-18 19:57:55

尝试以下代码:

代码语言:javascript
运行
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2019-11-18 20:21:09

我已经使用列表减少了变量的数量。我还为字符串添加了f标志,这样就不会滥用连接。总的来说,我稍微改进了你的代码(在视觉上) :)

代码语言:javascript
运行
复制
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并查看PEP8,这是Python语言的代码风格。

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

代码风格:https://www.python.org/dev/peps/pep-0008/

票数 1
EN

Stack Overflow用户

发布于 2019-11-18 20:08:27

最简单的方法。

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

代码语言:javascript
运行
复制
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) BTW.默认情况下,print中的参数以空格分隔。

对于Python 3.6+,您可以使用format-string,它更简单(请参阅'之前的f ):

print(f'\nWell done, Your final score is: {Score}')

print(f'What is {num3} + {num4}?')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58913904

复制
相关文章

相似问题

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