import random
target_number = 8
attempts = 0
for i in range(5): #本次游戏,最多可猜5次
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess == target_number:
print(f"恭喜你猜对了!你用了{attempts }次机会猜对啦!。")
break
elif guess < target_number:
print("你的猜测小了。")
else:
print("你的猜测大了。")
else:
print(f"很遗憾,你没有在规定次数内猜对,正确答案是{target_number}。")
运行结果: