我遇到了一个简单的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()
答案 0 :(得分:2)
您需要在代码的顶层调用quiz()
函数:
quiz()
你也失踪了:
import operator
答案 1 :(得分:1)
通过在最后添加一个调用语句来调用您的函数。quiz()
Python不像C或任何其他自动调用main
方法
在发现NPE时,您已将import operator
更改为import time
。改回来。