0

This probably is a really dumb question but when I type an answer, It is the correct answer, but it still says "incorrect, the answer is....." then prints out the answer I typed to begin with, so it isn't assigning 'rightanswer' to answer'. help?? (this is the newer version of python so the parenthesis are needed)

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

maths = (num1, operation, num2)

print (maths)

answer = input("what is the answer to the above sum?")

if operation == "/":
    CorrectAnswer = (num1/num2)
elif operation == "*":
    CorrectAnswer = (num1*num2)
elif operation == "-":
    CorrectAnswer = (num1-num2)
else:
    CorrectAnswer = (num1+num2)

time.sleep(1)
if answer == CorrectAnswer:
    print ("correct!")
else:
    print ("incorrect, the answer is", CorrectAnswer)
  • 2
    Since this almost certainly about your [GCSE programming problem](http://www.reddit.com/r/Python/comments/2gawvg/gcse_computing_programming_tasks_14_16_year_olds/), please do read [Open letter to students with homework problems](http://meta.programmers.stackexchange.com/q/6166). – Martijn Pieters Mar 10 '15 at 20:40
  • Side note: in your message to the user, "above sum" should only allow `+` operation problems, but obviously that's not the case. – Shashank Agarwal Mar 10 '15 at 20:45
  • To martijn Pieters - Don't mean to sound stuck up, yes I am a student but I am also 2 years before my GCSE's and there isn't a computing course available in my school so I decided to teach myself. Sorry if you think I wasted your time. – Jess Williams Mar 10 '15 at 21:24

2 Answers2

3

In Python 3, input() returns a string; strings are never equal to integers or floating point numbers:

>>> input('Enter the number 42: ') == 42
Enter the number 42: 42
False
>>> '42' == 42
False

Convert the answer to an integer or float first before testing it against your calculated CorrectAnswer integer:

answer = int(input("what is the answer to the above sum?"))

The int() and float() functions can do this conversion for you, but take into account that they'll raise a ValueError exception if the input is not convertible.

See Asking the user for input until they give a valid response if you want to handle incorrect input better, by catching that exception and re-prompting for an answer.

If you are going to use float(), take into account that floating point values are approximations, and a user typing in 0.33 while your 1 / 3 expression actually produces 0.3333333333333333 is likely to lead to issues:

>>> 1/3
0.3333333333333333
>>> float('0.33') == 1/3
False

See What is the best way to compare floats for almost-equality in Python? for approaches to how to handle this.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

input will return a string, but CorrectAnswer is a numeric type, such as float. You should convert answer to a float and then compare -

if float(answer) == CorrectAnswer

Shashank Agarwal
  • 2,769
  • 1
  • 22
  • 24