[英]How to quit on a python quiz
这是我必须做的一个基本数学测验,但我还没有弄清楚如何让游戏退出。 我已经做到了,每当您按 -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)
我使用列表减少了变量的数量。 我还在字符串中添加了 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 并查看 PEP8,python 语言代码样式。
OOP: https://realpython.com/python3-object-oriented-programming/
最简单的方式海事组织。
我刚刚添加了if ans == 1: break
after each input
。
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
如果 arguments 用空格隔开就好了,例如。 random.randint(1, 12)
function 之后的空格(如 Python 3 中的input
或print
)是不必要的。
您可以加入新行和字符串,例如。 print('\nWell done, Your final score is:', Score)
BTW。 print
中的 arguments 默认用空格分隔。
对于 Python 3.6+,您可以使用格式字符串,它更容易(参见f
之前'
):
print(f'\nWell done, Your final score is: {Score}')
或print(f'What is {num3} + {num4}?')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.