[英]Simple Python 3 addition game
我正在用 Python 编写一个简单的数学游戏,让用户输入一个加法问题,直到他们得到正确答案
这是我试过的代码:
import random
def problem():
number_one = random.randrange(1, 21)
number_two = random.randrange(1, 21)
problem = str(number_one) + " + " + str(number_two)
solution = number_one + number_two
user_solution = get_user_solution(problem)
while user_solution != solution:
get_user_solution(problem)
if user_solution == solution:
print('You are correct')
def get_user_solution(problem):
print("Enter your answer")
print(problem, end="")
result = int(input(" = "))
return result
problem()
如果用户立即输入正确答案,或者当用户输入错误答案时,它似乎有效,但如果用户首先输入错误答案,然后输入正确答案,则如下所示
因此,在您的 while 循环中,当您调用函数 get_user_problem(problem) 时,您不会将 user_solution 更新为用户输入的答案。 您不断检查的答案是用户第一次输入任何内容,因为这是您存储在 user_solution 中的内容,如果这不是正确答案,则会陷入无限循环。 此外,您不需要最后一个条件,因为它只会在您的用户输入正确答案时退出 while 循环,因此您不必再次检查!
这是您更正的代码:
import random
def problem():
number_one = random.randrange(1, 21)
number_two = random.randrange(1, 21)
problem = str(number_one) + " + " + str(number_two)
solution = number_one + number_two
user_solution = get_user_solution(problem)
while user_solution != solution:
user_solution = get_user_solution(problem)
#you could add some feedback here and say "incorrect answer"
print("You are correct!")
def get_user_solution(problem):
print("Enter your answer")
print(problem, end="")
result = int(input(" = "))
return result
"""
you could also house this call in a if __name__ == '__main__': conditional, look it up, may help in the future!
"""
problem()
让我知道这个是否奏效!
您缺少对while
循环内变量的赋值:
import random
def problem():
number_one = random.randrange(1, 21)
number_two = random.randrange(1, 21)
problem = str(number_one) + " + " + str(number_two)
solution = number_one + number_two
user_solution = get_user_solution(problem)
while user_solution != solution:
user_solution = get_user_solution(problem)
if user_solution == solution:
print('You are correct')
def get_user_solution(problem):
print("Enter your answer")
print(problem, end="")
result = int(input(" = "))
return result
problem()
import random
def problem():
number_one = random.randrange(1, 21)
number_two = random.randrange(1, 21)
problem = str(number_one) + " + " + str(number_two)
solution = number_one + number_two
user_solution = get_user_solution(problem)
while True:
if get_user_solution(problem) == solution:
print('You are correct')
break
def get_user_solution(problem):
print("Enter your answer")
print(problem, end="")
result = int(input(" = "))
return result
problem()
试试这个。
您想更新 while 循环内的 user_solution 值。
import random
def problem():
number_one = random.randrange(1, 21)
number_two = random.randrange(1, 21)
problem = str(number_one) + " + " + str(number_two)
solution = number_one + number_two
user_solution = get_user_solution(problem)
while user_solution != solution:
user_solution = get_user_solution(problem)#you want to update the value of user_solution
#if user_solution == solution: that is useless because only if while loop is completed,comes to this step
print('You are correct')
def get_user_solution(problem):
print("Enter your answer")
print(problem, end="")
result = int(input(" = "))
return result
problem()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.