我正在制作一个符合以下标准的乘法导师计划:
程序会询问用户他们想要解决的问题,如果是 用户输入的值大于10,程序将数字解释为 错误并提示用户输入其他值。
程序使用两个变量生成随机乘法问题 0到12之间的数字。
程序打印问题并提示用户回答问题,如果是 用户没有正确回答它,程序会告诉用户是否 他的答案太高或太低,然后问了一个新问题。
我在这个网站上找到了以下代码,并认为我可以修改它以上述方式工作:
import random
import math
game = int(input("How many problems do you want?\n"))
num_1 = random.randint(1,10)
num_2 = random.randint(1,10)
def main(game):
random.seed()
count = 0
correct = 0
#wrong != 0 #Took this variable out and replaced it with "if guess != answer" ('wrong' was originally part of an else statement) because it was not working properly.
while count < game:
num_1 = random.randint(1,10)
num_2 = random.randint(1,10)
guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
answer = str(num_1*num_2)
count += 1
if guess == answer:
correct += 1
print1("Correct!")
if guess != answer:
print("Sorry, the answer is", answer, ".")
if game > 1:
result = correct * 100./game
print("You got ", "%.1f"%result, "of the problems.")
main()
但.... 我一直收到以下错误,不知道该怎么办:
Traceback (most recent call last):
File "C:\Users\Internet Fate\Desktop\game.py", line 30, in <module>
main()
TypeError: main() missing 1 required positional argument: 'game'
我一直试图找出问题已经超过一个小时了,我开始感到压力。除此之外,我不知道如何让程序只接受1-10的输入游戏的次数(参考标准点第一)。如何让程序告诉用户他们的答案太高或太低,然后让它给他们一个新问题(参见第三点)。并且不知道如何重复此过程指定的次数,然后重新启动。
答案 0 :(得分:0)
将最后main()
替换为main(game)
并做出这些小改动,记得阅读python教程,祝你好运!
import random
import math
game = int(input("How many problems do you want?\n"))
num_1 = random.randint(1,10)
num_2 = random.randint(1,10)
def main(game):
random.seed()
count = 0
correct = 0
result = 0 #Here we initialized result to 0
while count < game:
num_1 = random.randint(1,10)
num_2 = random.randint(1,10)
guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
answer = num_1*num_2 # Here we don't convert to string so the next "if" works
count += 1
if guess == answer:
correct += 1
print("Correct!")
if guess != answer:
print("Sorry, the answer is", answer, ".")
if game > 1:
result = correct * 100./game
print("You got ", "%.1f"%result, "of the problems.")
main(game) #send game as parameter