-4

Im having trouble with a simple python quiz that randomly generates the answers and operators I'm fairly new to python and this is as complex as I can get, The program will run with no errors but will not do anything any help would be appreciated.

Here is the code (taken from 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()
4
  • @Cyber Why didn't you call the function in your answer. The OP is disgusted with you ;) Jan 15, 2015 at 19:14
  • 1
    @BhargavRao I'm sorry! I'm driving over to their computer right now to type it for them! Jan 15, 2015 at 19:16
  • @Cyber: Wow, that explains it! (I couldn't help wondering how the OP could write such high-quality code yet not know how to call it.)
    – NPE
    Jan 15, 2015 at 19:17
  • @Cyber That is the best solution. Better do it fast. Also ask the OP, as to how operator became time Jan 15, 2015 at 19:17

2 Answers 2

2

You need to call the quiz() function at the top level of your code:

quiz()

You're also missing:

import operator
1

Call your functions by adding a call statement at the end.quiz()

Python is not like C or any other language which automatically calls the main method

As NPE spotted, you have changed import operator to import time. Change it back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.