Is there a way to make an integer input output a certain thing when a sting or float is inputted? [duplicate]

Solution 1:

while True:
    try: #Use the try/except block to raise exception if value is not integer
        fav_number = int(input("Guess my favorite number: "))
        print()
    except ValueError: # if value is anything but integer, exception is raised
        print("Invalid entry. Try again.")
    else:
        if fav_number == 8:
            print("Gongrats, you guessed it!")
            break #code ends when number is guessed
        else:
            print("Nope, try again!")
            

To make it more interesting, you can add a predefined number of attempts. If attempts are exhausted, code stops:

attempts = 5
while attempts > 0:
    attempts -= 1
    try: #Use the try/except block to raise exception if value is not integer
        fav_number = int(input("Guess my favorite number: "))
        print()
    except ValueError: # if value is anything but integer, exception is raised
        print("Invalid entry. Try again.")
    else:
        if attempts > 0:
            if fav_number == 8:
                print("Gongrats, you guessed it!")
                break #code ends when number is guessed
            else:
                print("Nope, try again!")
                print(f"You have {attempts} left")
        else:
            print("You have exhausted your attempts.")