I am learning python and was writing this game, where the program picks a random number and asks the user to guess the number. In this case, a user can enter an empty string or an alphabet by mistake. So, i felt the need to check the type of the user input before doing the comparison as shown below. Also, this lead me to the following stackoverflow entry Checking whether a variable is an integer or not
My question is why checking of types is considered a bad practice and how can i accomplish my task by not checking the type?
import random
num = random.randint(1,10)
while True:
guess = input('Guess a number between 1 and 10: ')
if(bool(guess)):
try:
guess = int(guess)
except:
print("Please enter a numeric value")
continue
else:
print("you have to enter some number")
continue
if guess == num:
print("you guessed it right")
break
elif guess < num:
print("Try higher")
elif guess > num:
print("Try lower")