I want to assign something a value and then try and get someone to guess that value. I have tried something along the lines of this but I can’t seem to get it to work…:
foo = 1
guessfoo = input('Guess my number: ')
if foo == guessfoo:
print('Well done, You guessed it!')
if foo != guessfoo:
print('haha, fail.')
Why doesn’t this work? And how should I be doing it? I am a beginner at this, please help!
With python version 3 :
input() returns a ‘str’ (string) object. A string compares to an integer returns False :
You must cast your input like that :
Don’t forget to try…except if the result of the input cannot be casted into an int.
Full example code :
EDIT:
With python version 2 :
Thanks for this comment :
Ouput or your original code: