I tried to make my first game on python. There's something I don't understand because when I try enter "hint" has an answer, I get this error code:
Traceback (most recent call last):
File "C:/Users/Lenovo/PycharmProjects/Test/while loop test.py", line 18, in <module>
if int(answer) == secret_number:
ValueError: invalid literal for int() with base 10: 'hint'
Can you give me any idea what i've done wrong? I tried to reassign the type string to the variable "answer" but it doesn't work.
Here's the code:
# Guessing game
import random
# Game explanation
print("Welcome to the Guessing game ! \nYou have 3 chances to find the number between 1 and 10, otherwise it ends!")
# Game variables
secret_number = random.randint(1, 11)
random_hint_number = random.randint(1, 5)
secret_number_hint = ("The secret number is between ", (secret_number - random_hint_number), (secret_number + random_hint_number))
guess_count = 0
guess_limit = 3
# game engine
while guess_count < guess_limit:
answer = input("What is your guess?")
guess_count += 1
if int(answer) == secret_number:
print("You won!")
break
elif answer == "hint":
print(f"{secret_number_hint}")
guess_count -= 1
elif int(answer) != secret_number:
print("Wrong answer")
print("If you need an hint, type in: hint")
print("Sorry you failed")
You'll need to test the input in the proper order
if answer == "hint":
print(f"{secret_number_hint}")
guess_count -= 1
continue
if int(answer) == secret_number:
print("You won!")
break
else:
print("Wrong answer")
print("If you need an hint, type in: hint")