0
import random
random_number = random.randint(1, 99)
user_guess = 1
print(random_number)

while user_guess != random_number:
user_guess = int((input('Guess the number between 1 and 99: ')))

if user_guess == (''):
print('\\nWe need a number please!\\n')

if user_guess >= 0:
if user_guess > random_number:
print('\\nNumber too large!\\n')
elif user_guess < random_number:
print('\\nNumber too small!\\n')

else:
print('\nCongratulations!!!  \nYou\'ve picked the correct number.')

This is the issue is, if the user presses enter in the input without entering a number.

I understand when return is pressed without entering a value, it returns a string, but the input needs to be an integer. I get the following error: ValueError: invalid literal for int() with base 10: ''

I'm teaching myself Python, very new, and would welcome some help to fix the issue.

Sanjay
  • 25
  • 4
  • 1
    Defer the `int( ... )` call until after your `if` has validated that the user input is not the empty string. As you observed, the empty string is not a valid integer so it will cause `int()` to raise an error. – J_H Jan 14 '23 at 08:38

0 Answers0