1

I'm a beginner in Python and I'm basically trying to make a basic maths program for myself and started off with a two-digit addition program with random questions each time and I'm even managing to mess that up. Each time I input a correct answer it says it's incorrect everytime. Here's the code:

import random

digit_one = random.randint(0,100)
digit_two = random.randint(0,100)


print "What is %s + %s?" % (digit_one, digit_two)

answer = digit_one + digit_two    

userAnswer = raw_input()

if userAnswer == answer:
    print "Well done that's correct!"

else:
    print "That's incorrect!"

I'm working in 2.7 and the built-in IDLE IDE. I have a feeling I should have used a loop for this instead although I know I'll have to use one later on if I want it to keep reloading questions. I'd appreciate any help, thank you.

5
  • 3
    @Hyperboreus: Hell no. Call int on the string. input is a horrible habit to get into. Commented Jan 24, 2014 at 19:21
  • @Hyperboreus I had to fight the urge to flag your comment as offensive... but seriously, no. Please don't go recommending things like python2s input or any other variation of eval to beginners without being extremely clear about all security and performance implications this has. And especially, don't do it in cases where there is a perfectly safe, fast, readable and obvious way to achieve the same task (which is int in this case). Commented Jan 24, 2014 at 19:31
  • @l4mpi "Offensive"? Whom did I offend? I am a strong advocate against the use of eval and even literal_eval as can be seen from my past answers. I personally never use input (the py2 one). BUT, it seems to me, that a lot of tutorials seem to use it, because most (or at least very many) beginners questions use it. So I suspected that input was quite common among beginners as your input gets parsed nicely and magically. And later one starts learning about types, type conversion, autoboxing, comparison, identity, etc... No intention to offend anyone. Commented Jan 24, 2014 at 19:49
  • @Hyperboreus The "offensive" part wasn't all that serious, thus the "but seriously" afterwards. And the target group for being "offended" would be everyone on SO dealing with entry level python questions and telling people to stop using input/eval over and over again ;) Anyways, you contradict yourself - if you're a strong advocate against eval, why recommend input which uses it? Especially to a beginner who should rather be steered away from bad habits like this. Feels like a C programmer who, faced with a question about how to repeat statements, recommends goto over for... Commented Jan 24, 2014 at 21:51
  • @l4mpi Thank you for the clarification. You are right. I have deleted the offending comment. Commented Jan 24, 2014 at 22:42

4 Answers 4

3
userAnswer = raw_input()

raw_input returns a string. You need to explicitly convert that to int like this

userAnswer = int(raw_input())

since you are comparing it against answer, which is an int. You can confirm this by printing the types of those variables, like this

print type(answer), type(userAnswer)
Sign up to request clarification or add additional context in comments.

Comments

3

raw_input() returns a string (str). In your conditional you are comparing whether a str is equals to answer (int).

To fix this you could convert the input to an int:

userAnswer = int(raw_input())

1 Comment

Oh god that was so basic. This solution actually ran through my head briefly as well. Thanks for the help.
2

raw_input returns a string. Turn it into an int to compare it with numbers:

>>> x = raw_input()
23
>>> x
'23'
>>> 23
23
>>> x == 23
False
>>> int(x)
23
>>> int(x) == 23
True

Comments

0

You are trying to compare a string with an integer. Try inputting 'userAnswer' as an integer, rather than a string, like so:

userAnswer = int(raw_input())

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.