随机算术测验问题

Random Arithmetic Quiz Problems

我在做一个随机生成答案和运算符的简单 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()

您需要在代码的顶层调用 quiz() 函数:

quiz()

你还想念:

import operator

通过在末尾添加调用语句来调用您的函数。quiz()

Python 不像 C 或任何其他自动调用 main 方法的语言

正如 NPE 发现的那样,您已将 import operator 更改为 import time。改回来。