我有一个简单的Python测验,随机生成答案和操作符的麻烦我对python相当新,这是尽可能复杂,我可以得到,该程序将运行没有错误,但不会做任何帮助将不胜感激。随机算术测验问题
这里是(从How can I randomly choose a maths operator and ask recurring maths questions with it?所)代码:
import random
import time
def randomCalc():
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv}
num1 = random.randint(0,12)
num2 = random.randint(1,10)
op = random.choice(list(ops.keys()))
answer = ops.get(op)(num1,num2)
print('What is {} {} {}?\n'.format(num1, op, num2))
return answer
def askQuestion():
answer = randomCalc()
guess = float(input())
return guess == answer
def quiz():
print('Welcome. This is a 10 question math quiz\n')
score = 0
for i in range(10):
correct = askQuestion()
if correct:
score += 1
print('Correct!\n')
else:
print('Incorrect!\n')
return 'Your score was {}/10'.format()
致电你的职能 –
@Cyber你为什么没有在你的答案中调用该函数。 OP对你感到厌恶;) –
@BhargavRao对不起!我现在开车到他们的电脑为他们输入它! – CoryKramer