I need to write a code that checks that the user input is a positive integer between 1 and 9. And if a string or negative integer is provided the program will display a message that reads "You have not entered a valid number - 0 has been stored instead." The code below does work. But if the user input is -5 the error message displays twice and I only need it to display once. I'm doing something wrong, but I don't know what.
try:
x = input("Enter the first number:")
x = int(x)
except:
print("You have not entered a valid number - 0 has been stored instead")
x = 0
if x < 1 or x > 9:
print("You have not entered a valid number - 0 has been stored instead")
x = 0
print(x)