在我的第一个简单 python 猜数游戏中添加 "hint"

Add an "hint" to my first simple python number guessing game

我试图在 python 上制作我的第一款游戏。有些东西我不明白,因为当我尝试输入 "hint" 有答案时,我得到这个错误代码:


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'

你能告诉我我做错了什么吗?我试图将类型字符串重新分配给变量 "answer" 但它不起作用。
这是代码:

# 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")

您需要按正确的顺序测试输入

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")