Reputation: 3
I made a simple guess the number program, in my quest to learn python! lol
however, the computer will not acknowledge the user's guess was correct, and it will always go to the "incorrect", even though the computers number displays as the users guess. My code seems correct, so i am at a loss. Any help is appreciated!
def randomNumGen():
userGuess = input('The Computer has thought of a number between 1-5, deposit your guess now!: ')
if finalValue == userGuess:
print("You've guessed correctly!, Very lucky you are!")
else:
print("You're guess was incorrect, try again please")
print("The computer's guess was",finalValue)
main()
EDIT:
the output looks like this, which explains the obvious issue here:
>The Computer has thought of a number between 1-5, deposit your guess now!: 2
>You're guess was was incorrect
>The computer's guess was 2
so the 'computer' will not recognize a correct guess when one has been given.
Upvotes: 0
Views: 58
Reputation: 11935
Most likely finalValue
is an integer, while the input from input()
will be a string. You should make both into strings, or both into integers before you compare them.
To convert an integer to a string, you can simply do this:
my_string = str(my_integer)
This will take an integer named my_integer
and return a string which will be assigned to be my_string
def randomNumGen():
userGuess = input('The Computer has thought of a number between 1-5, deposit your guess now!: ')
if str(finalValue) == userGuess:
print("You've guessed correctly!, Very lucky you are!")
else:
print("You're guess was incorrect, try again please")
print("The computer's guess was",finalValue)
main()
This is the simplest change I can see that will make this work. Note the line:
if str(finalValue) == userGuess:
I am comparing finalValue
cast into a string with the userGuess
.
Upvotes: 3
Reputation: 7422
I believe the issue is that your input is being processed as a string when it needs to be recognized as an integer. Try this:
def randomNumGen():
userGuess = input('The Computer has thought of a number between 1-5, deposit your guess now!: ')
userGuess = int(userGuess)
if finalValue == userGuess:
print("You've guessed correctly!, Very lucky you are!")
else:
print("You're guess was incorrect, try again please")
print("The computer's guess was",finalValue)
main()
Upvotes: 0