【发布时间】:2015-01-15 19:07:58
【问题描述】:
我在一个简单的 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 对不起!我现在正开车到他们的电脑前为他们打字!
-
@Cyber:哇,这就解释了! (我不禁想知道 OP 是如何编写如此高质量的代码却不知道如何调用它的。)
-
@Cyber 这是最好的解决方案。最好快点做。还要问OP,
operator是怎么变成time的