+ 3

what is wrong with my code?

Here is the code but i can't figure out what is wrong with in (in python): #!/usr/bin/env python '''guesser game''' import random def main(): randomNumber = random.randint(1,100) found = False while not found: UserGuess = input("Your guess: ") if UserGuess == randomNumber: print("You guessed it!!") found = True elif UserGuess < randomNumber: print("Guess higher!!") else: print("Guess lower!!") if __name__ == "__main__": main()

1st Jan 2018, 7:29 PM
Julian
Julian - avatar
7 Answers
+ 4
Once the conversion to int change is made, your code works fine for me without any errors on my computer. If you're running this in the playground, you have to put all of your guesses in at the same time at the prompt. If none of those guesses is correct, then the you'll receive an EOF error because the program is expecting further user input but doesn't receive it. This is just how the code playground handles it and doesn't mean that there is an actual issue with your code. Try making the random number range smaller like 1-10 and then enter all the possible answers. You'll see that the code runs fine without error then.
1st Jan 2018, 7:52 PM
ChaoticDawg
ChaoticDawg - avatar
+ 8
UserGuess is string, a default type returned by input() and you compare it with an integer (a product of randint() They will never match :)
1st Jan 2018, 7:42 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
UserGuess = int(input("Your guess: "))
1st Jan 2018, 7:41 PM
richard
+ 2
Ooooo thx
1st Jan 2018, 7:41 PM
Julian
Julian - avatar
+ 2
UserGuess = input("Your guess: ") if UserGuess == randomNumber: Here you're comparing a str to a number (int) You need to convert the input (UserGuess) to an int prior to the comparison. UserGuess = int(input("Your guess: "))
1st Jan 2018, 7:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
What is the error? Where are you trying to run the code?
1st Jan 2018, 7:44 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Its an eof error. It has an eof when reading a line
1st Jan 2018, 7:46 PM
Julian
Julian - avatar