-5

This is my code it has to have + - * in the code and it has to be chosen randomly but it does not work it does not say the correct answer I would appreciate any help thanks.

import random
import operator
question_number = 0
score = 0

ops = {'+':operator.add,
           '-':operator.sub,
           '*':operator.mul,
           '/':operator.truediv}
number1 = random.randint(0,12)
number2 = random.randint(1,10)
op = random.choice(list(ops.keys()))

print ("this is a short maths quiz")
name = input ("what is your name")
age = input ("how old are you " +name)
print ("ok "+name+" im going to start the quiz now")
print(number1, op, number2)
user_input=int(input())
answer = (number1,op,number2)
if user_input == answer:
    print("Well done")
    score = score + 1

else:
    print("WRONG!")
    print("The answer was",answer)

question_number = question_number + 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • You've got to get more specific than that, please. – runDOSrun Feb 22 '15 at 16:40
  • Try `print(answer)` and consider whether it's *ever* going to be equal to an integer... – jonrsharpe Feb 22 '15 at 16:44
  • possible duplicate of [Trying to loop just some parts of a math quiz program](http://stackoverflow.com/questions/7464345/trying-to-loop-just-some-parts-of-a-math-quiz-program). It seems like every day, four times a day we get a random math quiz problem. – Malik Brahimi Feb 22 '15 at 16:50
  • @MalikBrahimi, it may be a similar but there is no relation to what the OP is using why their code does not work. – Padraic Cunningham Feb 22 '15 at 16:54

2 Answers2

0

You need to op as a key to get the appropriate value from the ops dict and call it on the two numbers:

answer = ops[op](number1, number2)

Your code is comparing an int to a tuple i.e 9 == (3, '+', 6)

You may also want to keep the larger number on the left and smaller on the right unless you want negative numbers.

answer = ops[op](max(number1,number2),min(number1, number2))

Also unless this is in a while loop question_number = question_number + 1 is not going to do much.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

you need to make the op an op and not a string. this is your code fixed.

import random
import operator

question_number = 0
score = 0

ops = {'+':operator.add,
           '-':operator.sub,
           '*':operator.mul,
           '/':operator.truediv}
number1 = random.randint(0,12)
number2 = random.randint(1,10)
op = random.choice(list(ops.keys()))

print ("this is a short maths quiz")
name = input ("what is your name")
age = input ("how old are you " +name)
print ("ok "+name+" im going to start the quiz now")
print(number1, op, number2)
user_input=int(input())
if op == "+":
    answer = (number1+number2)
elif op == "-":
    answer = (number1-number2)
elif op == "*":
    answer = (number1*number2)
elif op == "/":
    answer = (number1/number2)
if user_input == answer:
    print("Well done")
    score = score + 1

else:
    print("WRONG!")
    print("The answer was",answer)

question_number = question_number + 1

you could add a while loop to make it repeating

Liam
  • 6,009
  • 4
  • 39
  • 53