2015-01-15 35 views
-4

我有一个简单的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

致电你的职能 –

+0

@Cyber​​你为什么没有在你的答案中调用该函数。 OP对你感到厌恶;) –

+1

@BhargavRao对不起!我现在开车到他们的电脑为他们输入它! – CoryKramer

回答

2

你需要调用quiz()功能在你的代码的顶层:

quiz() 

你还缺少:

import operator 
1

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

Python是不是像C或作为NPE发现它会自动调用main方法

任何其他语言,你已经改变了import operatorimport time。改回它。