-1

I have an exercise from the course I study from, but it gives me an error:

"Guess a number between 1 and 100: Traceback (most recent call last):
  File "main.py", line 15, in <module>
    guess = input("Guess a number between 1 and 100: ")
EOFError: EOF when reading a line"
  1. How can I fix that ?
  2. Have I done the exercise in a right way ? And just to make sure - the "break" breaks the while, right ?
# Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:

# If a player's guess is less than 1 or greater than 100, say "OUT OF BOUNDS"
# On a player's first turn, if their guess is #within 10 of the number, return "WARM!"
# further than 10 away from the number, return "COLD!"
# On all subsequent turns, if a guess is
# closer to the number than the previous guess return "WARMER!"
# farther from the number than the previous guess, return "COLDER!"
# When the player's guess equals the number, tell them they've guessed correctly and how many guesses it took!

from random import randint

random_number = randint(1,100)
guess_count = 0
guess = input("Guess a number between 1 and 100: ")
while False:
    guess_count += 1
    if guess_count == 1:
        if guess == random_number:
            print(f'Congratulations! You have chose the correct number after the first try!')
            break
        else:
            if abs(guess-random_number) < 11:
                print("WARM!")
            else:
                print("COLD!")
    else:
        old_guess = guess
        guess = input("Guess another number between 1 and 100: ")
        if guess == random_number:
            print(f'Congratulations! You have chose the correct number after {guess_count} tries!')
            break
        elif abs(random_number - guess) < abs(random_number - old_guess):
            print('WARMER!')
        elif abs(random_number - guess) > abs(random_number - old_guess):
            print('COLDER!')





input("Press anywhere to exit ")
Avishay Cohen
  • 1,978
  • 2
  • 21
  • 34
Aviran
  • 49
  • 1
  • 4

1 Answers1

1

The reason that you are getting

Traceback (most recent call last): File "main.py", line 15, in guess = input("Guess a number between 1 and 100: ") EOFError: EOF when reading a line"

could be because you have spaces or newline before actual numbers.

Consider evaluating the user input (what if user enters a character? ) before using it. Take a look at How can I read inputs as numbers? as an example.

Also like others have pointed out, change while False:to while True:

Hope this helps

foo
  • 71
  • 2
  • 8