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
|