Yes, you are absolutely right that "5"
is distinct from 5
. You can convert 5
into a string by using str(5)
. An alternative would be to convert "5"
into an integer by int("5")
but that option can fail, so better handle the exception.
So, the change to your program could be e.g. the following:
if guess == str(a):
instead of:
if guess == a:
Another option would be to convert guess into an integer, as explained in the other answer.
EDIT: This only applies to Python versions 2.x:
However, you're using input()
, not raw_input()
. input()
returns an integer if you type an integer (and fails if you type text that isn't a valid Python expression). I tested your program and it asked What is 4 - 2?
; I typed 2
and it sait Correct!
so I don't see what is your problem.
Have you noticed that if your program asks What is 9 - 4?
you can type 9 - 4
and it says Correct!
? That's due to you using input()
, not raw_input()
. Similarly, if you type e.g. c
, your program fails with NameError
I would however use raw_input()
and then compare the answer to str(correct_answer)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…