6

I have a simple maths task I'm having problems executing, involving the random import. The idea is that there is a quiz of 10 randomly generated questions. I've got the numbers ranging from (0,12) using the random.randint function, that works fine. Its the next bit of choosing a random operator I'm having problems with ['+', '-', '*', '/'].

I have my more sophisticated coding back at school, but this is my practise one that all I need is the ability to randomly create a question and ask it, whilst also being able to answer it itself to determine if the answer given is correct. Here's my code:

import random

ops = ['+', '-', '*', '/']
num1 = random.randint(0,12)
num2 = random.randint(0,10)
operation = random.choice(ops)

print(num1)
print(num2)
print(operation)

maths = num1, operation, num2

print(maths)

As of right now though, my output is a little messed up. For example:

3
6
*
(3, '*', 6)

Clearly it can't determine the answer from (3, '*', 6). I'll be turning this operation into a subroutine in my other program, but it needs to work first!

And forgive me if its not very well done, this was a quick recreation of the task I left at school, and I'm also fairly new at this with limited knowledge. Thanks in advance!

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Obahar
  • 107
  • 1
  • 1
  • 10
  • Don't you just want `print(num1, operation, num2)` (or `print(*maths)`)? Could you clarify the output you're expecting? – jonrsharpe Oct 08 '14 at 15:41
  • You should edit your question so as to _actually ask a question._ – DaoWen Oct 08 '14 at 15:43
  • Well, the idea is that it will then ask the question whilst knowing the question to be able to answer it itself and it'll reset itself after each question so I assumed a variable would be useful for both situations. And I'm pretty sure that is a question, just poorly written? But I'll try rewording it. Thanks. – Obahar Oct 08 '14 at 15:49

2 Answers2

25

How about you make a dictionary that maps the operator's character (e.g. '+') to the operator (e.g. operator.add). Then sample that, format you string, and perform the operation.

import random
import operator

Generating a random mathematical expression

def randomCalc():
    ops = {'+':operator.add,
           '-':operator.sub,
           '*':operator.mul,
           '/':operator.truediv}
    num1 = random.randint(0,12)
    num2 = random.randint(1,10)   # I don't sample 0's to protect against divide-by-zero
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1,num2)
    print('What is {} {} {}?\n'.format(num1, op, num2))
    return answer

Asking the user

def askQuestion():
    answer = randomCalc()
    guess = float(input())
    return guess == answer

Finally making a multi-question quiz

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(score)

Some testing

>>> quiz()
Welcome. This is a 10 question math quiz

What is 8 - 6?
2
Correct!

What is 10 + 6?
16
Correct!

What is 12 - 1?
11
Correct!

What is 9 + 4?
13
Correct!

What is 0 - 8?
-8
Correct!

What is 1 * 1?
5
Incorrect!

What is 5 * 8?
40
Correct!

What is 11 / 1?
11
Correct!

What is 1 / 4?
0.25
Correct!

What is 1 * 1?
1
Correct!

'Your score was 9/10'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Use a list for the operators e.g operator = ['+', '',' -', '/'] then you can use Then you can use random choice on your list to call a random operator (+,-,,/) x = (random.choice(operator)) Finally you will need to convert your num1 & num2 to strings something like this eval(str(num1)+ x + str(num2)) That should make your quiz completly random

Wayne Askey
  • 13
  • 1
  • 4