"""
猜数字游戏
计算机随机生成一个数字,玩家尝试猜测
"""
import random
import time
def guess_number_game():
"""猜数字游戏主程序"""
print("🎮 欢迎来到猜数字游戏!")
print("=" * 50)
print("游戏规则:")
print("1. 我会随机生成一个1-100之间的数字")
print("2. 你有10次机会猜对这个数字")
print("3. 每次猜测后,我会告诉你猜大了还是猜小了")
print("4. 看看你能用几次猜中!")
print("=" * 50)
while True:
# 生成随机数字
secret_number = random.randint(1, 100)
max_attempts = 10
attempts = 0
print(f"\n🤔 我已经想好了一个1-100之间的数字,你有{max_attempts}次机会!")
while attempts < max_attempts:
try:
attempts += 1
remaining = max_attempts - attempts + 1
# 获取玩家猜测
guess = int(input(f"\n第{attempts}次尝试 (剩余{remaining}次): 请输入你的猜测: "))
# 检查猜测范围
if guess < 1 or guess > 100:
print("⚠️ 请输入1-100之间的数字!")
attempts -= 1
continue
# 判断猜测结果
if guess < secret_number:
print(f"🔺 太小了!再试试更大的数字")
elif guess > secret_number:
print(f"🔻 太大了!试试更小的数字")
else:
print("🎉" * 20)
print(f"🎉 恭喜你!猜对了!数字就是 {secret_number}")
print(f"🎉 你用了 {attempts} 次猜中")
# 根据尝试次数给出评价
if attempts == 1:
print("🎉 天才!一次就中!")
elif attempts <= 3:
print("🎉 太棒了!你真是个猜数字高手!")
elif attempts <= 6:
print("🎉 很不错!继续努力!")
else:
print("🎉 恭喜猜中!")
print("🎉" * 20)
break
# 提示范围
if attempts >= 3:
if secret_number < 50:
print("💡 提示:数字在1-50之间")
else:
print("💡 提示:数字在51-100之间")
if attempts >= 7:
if secret_number % 2 == 0:
print("💡 提示:这个数字是偶数")
else:
print("💡 提示:这个数字是奇数")
except ValueError:
print("⚠️ 请输入有效的数字!")
attempts -= 1
except KeyboardInterrupt:
print("\n\n游戏被中断!")
return
# 如果机会用完
if attempts >= max_attempts and guess != secret_number:
print("\n💀" * 20)
print(f"💀 很遗憾,{max_attempts}次机会用完了!")
print(f"💀 正确答案是: {secret_number}")
print("💀" * 20)
# 询问是否继续游戏
while True:
play_again = input("\n是否再玩一局?(y/n): ").lower()
if play_again in ['y', 'yes', '是', '继续']:
print("\n" + "="*50)
print("新游戏开始!")
break
elif play_again in ['n', 'no', '否', '退出']:
print("感谢游玩!再见!")
return
else:
print("请输入 'y' 继续或 'n' 退出")
# 运行猜数字游戏
if __name__ == "__main__":
guess_number_game()