VIRUS_2091 发表于 3 天前

(跟甲鱼哥学的)猜数字游戏-优化版

import random
LOWER_BOUND = 1
UPPER_BOUND = 100
secret_number = random.randint(LOWER_BOUND, UPPER_BOUND)
print(f"Now I'm thinking about a number that ranges from {LOWER_BOUND} to {UPPER_BOUND} ")
print("Guess what it is!")

def check_guess(guess, secret):
    if guess < secret:
      return "Too small! Try a larger one~"
    elif guess > secret:
      return "Too large! Try a smaller one~"
    else:
      return "Congratulations! You've found out the corresponding number!"
attempts = 0
max_attempts = 10

print(f"You have {max_attempts} chances. Here we start!\n")

while attempts < max_attempts:
    try:
      guess = int(input("Please input your assumption: "))
    except ValueError:
      print("Invalid number! Try enter a whole number~")
      continue

    attempts += 1
   
    result = check_guess(guess, secret_number)
    print(result)
   
    if guess == secret_number:
      print(f"{attempts} times till you get the answer! Well done! ")
      break
   
    remaining = max_attempts - attempts
    if remaining > 0:
      print(f"{remaining} chances remaining~ \n")
    else:
      print("Just wasted all your chances! ")
      print(f"The secret number is {secret_number}. Try again next time~ ")

FishC 发表于 3 天前

鱼油写得不错!整体逻辑清晰,函数封装也很棒。这里给几个小优化建议,让代码更“甲鱼风”:

1. 边界检查
如果用户输入的数字超出范围(比如 0 或 101),其实可以提前提醒,不用浪费猜测次数。在 int() 转换后加个判断:


if guess < LOWER_BOUND or guess > UPPER_BOUND:
    print(f"Please enter a number between {LOWER_BOUND} and {UPPER_BOUND}!")
    continue


2. 输入空值处理
用户直接回车时,input() 会得到空字符串,int() 会报错。虽然已经被 except 捕获,但可以更友好地提示“不能为空”:


guess_str = input("Please input your assumption: ")
if not guess_str.strip():
    print("Empty input! Please enter a whole number~")
    continue


3. 减少重复判断
当前代码里 check_guess 判断了大小,后面又用 guess == secret_number 判断是否成功。可以改为让函数返回一个元组,避免重复比较:


def check_guess(guess, secret):
    if guess < secret:
      return False, "Too small! Try a larger one~"
    elif guess > secret:
      return False, "Too large! Try a smaller one~"
    else:
      return True, "Congratulations! You've found out the corresponding number!"


然后在主循环里:


success, message = check_guess(guess, secret_number)
print(message)
if success:
    print(f"{attempts} times till you get the answer! Well done! ")
    break


4. 最最重要的:继续游戏
现在一次游戏结束就退出,很不“甲鱼”。可以加一个外层循环,问用户“是否再来一局”:


while True:
    # 原有游戏代码...
    play_again = input("Play again? (yes/no): ").strip().lower()
    if play_again != "yes":
      print("Bye bye~ See you next time!")
      break


整体代码结构已经很好,继续加油!记住:好代码不是写出来的,是改出来的~

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

VIRUS_2091 发表于 3 天前

这AI助理好啊{:5_93:}

FishC 发表于 3 天前

VIRUS_2091 发表于 2026-08-18 14:18
这AI助理好啊{:5_93:}

代码写得很不错,看得出你确实把“优化”落到实处啦!{:5_76:}

几个亮点值得点赞:
- 用了常量 LOWER_BOUND 和 UPPER_BOUND,代码更易维护
- 把判断逻辑封装成 check_guess 函数,清晰又复用
- 捕获 ValueError,不怕用户乱输入
- 剩余次数提示非常友好,游戏体验很好

如果要再“优化”一点点,可以考虑:
- 增加输入范围检查,比如用户输入 101 或 0,可以提示“超出范围”
- 玩完一局后,问一句“再来一局?”,方便连玩
- 每次猜完后显示一下“当前范围”的提示,帮用户缩小范围

不过整体已经很好了,逻辑紧凑、可读性强,继续加油!{:5_148:}

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

VIRUS_2091 发表于 3 天前

为了方便复制代码,在此重发一遍{:5_105:}

import random
LOWER_BOUND = 1
UPPER_BOUND = 100
secret_number = random.randint(LOWER_BOUND, UPPER_BOUND)
print(f"Now I'm thinking about a number that ranges from {LOWER_BOUND} to {UPPER_BOUND} ")
print("Guess what it is!")

def check_guess(guess, secret):
    if guess < secret:
      return "Too small! Try a larger one~"
    elif guess > secret:
      return "Too large! Try a smaller one~"
    else:
      return "Congratulations! You've found out the corresponding number!"
attempts = 0
max_attempts = 10

print(f"You have {max_attempts} chances. Here we start!\n")

while attempts < max_attempts:
    try:
      guess = int(input("Please input your assumption: "))
    except ValueError:
      print("Invalid number! Try enter a whole number~")
      continue

    attempts += 1
   
    result = check_guess(guess, secret_number)
    print(result)
   
    if guess == secret_number:
      print(f"{attempts} times till you get the answer! Well done! ")
      break
   
    remaining = max_attempts - attempts
    if remaining > 0:
      print(f"{remaining} chances remaining~ \n")
    else:
      print("Just wasted all your chances! ")
      print(f"The secret number is {secret_number}. Try again next time~ ")

FishC 发表于 3 天前

VIRUS_2091 发表于 2026-08-18 17:31
为了方便复制代码,在此重发一遍{:5_105:}

import...

收到,代码已复制保存,方便随时参考和测试。{:5_148:} 写得很清晰,继续加油!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: (跟甲鱼哥学的)猜数字游戏-优化版