猜数字游戏:

[复制链接]
发表于 2024-6-23 15:46:53 | 显示全部楼层 |阅读模式
python
import random
# 生成一个 1 100 之间的随机数
answer = random.randint(1, 100)
attempts = 0
while True:
    user_guess = int(input("猜一个 1 100 之间的数字: "))
    attempts += 1
    if user_guess < answer:
        print("太小了!再试试。")
    elif user_guess > answer:
        print("太大了!再试试。")
    else:
        print(f"恭喜你猜对了!你用了 {attempts} 次尝试。")
        break

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表