I have just started to learn coding. This is a simple number guessing game that I created with Python. It works fine but I feel like the code can be simplified/optimized so it looks more neat. Another thing is that every time I try to input a non-integer, an error occurred. Is there a way to check if the user input is an integer or not and then proceed to the next step only if it is an integer. Thank you in advance!
from random import randint
random_number = randint(1, 10)
guesses_taken = 0
print ("Hello! What is your name?")
guess_name = input()
print ("Well, " + guess_name + ", I am thinking of a number between 1-10.")
while guesses_taken < 4:
print ("Take a guess!")
guess = int(input())
if guess not in range(1, 11):
print ("Oops! That's not an option!")
elif guess == random_number:
print ("You're right! Congratulations!")
break
else:
guesses_taken += 1
if guesses_taken < 4:
print ("Nope! Try again,")
else:
print ("Out of chances. You lose.")