鱼C论坛

 找回密码
 立即注册
查看: 13|回复: 1

[作品展示] (重发)优化版甲鱼猜数字游戏~

[复制链接]
发表于 前天 17:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
依旧重发。。。
甲鱼课上讲的猜数字小游戏,但是优化版!
  1. import random
  2. LOWER_BOUND = 1
  3. UPPER_BOUND = 100
  4. secret_number = random.randint(LOWER_BOUND, UPPER_BOUND)
  5. print(f"Now I'm thinking about a number that ranges from {LOWER_BOUND} to {UPPER_BOUND} ")
  6. print("Guess what it is!")

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

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

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

  23.     attempts += 1
  24.    
  25.     result = check_guess(guess, secret_number)
  26.     print(result)
  27.    
  28.     if guess == secret_number:
  29.         print(f"{attempts} times till you get the answer! Well done! ")
  30.         break
  31.    
  32.     remaining = max_attempts - attempts
  33.     if remaining > 0:
  34.         print(f"{remaining} chances remaining~ \n")
  35.     else:
  36.         print("Just wasted all your chances! ")
  37.         print(f"The secret number is {secret_number}. Try again next time~ ")
复制代码


可以看看相较基础版猜数字的优化点~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 前天 17:56 | 显示全部楼层
这个优化版确实比基础版强了不少,咱们来一起看看它“优化”在哪里~


  • 增加了上下限常量:用LOWER_BOUNDUPPER_BOUND来定义范围,改起来方便,代码也更清晰。
  • 把判断逻辑封装成函数check_guess,主逻辑更简洁,也便于复用。
  • 限制了猜测次数max_attempts = 10,让游戏更有挑战性,不会无限猜下去。
  • 加入了try...except处理输入异常,输字母或小数不会直接报错崩溃,提示用户重新输入。
  • 每次猜完都会提示剩余次数,最后还会公布正确答案,用户体验更好。
  • 使用了f-string格式化输出,代码更简洁,字符串拼接更直观。


所以说,这个版本在代码结构、输入健壮性、游戏体验三个方面都做了优化,已经是一个完整又好玩的小项目啦!

  1. import random

  2. LOWER_BOUND = 1
  3. UPPER_BOUND = 100
  4. secret_number = random.randint(LOWER_BOUND, UPPER_BOUND)

  5. print(f"Now I'm thinking about a number that ranges from {LOWER_BOUND} to {UPPER_BOUND} ")
  6. print("Guess what it is!")

  7. def check_guess(guess, secret):
  8.     if guess < secret:
  9.         return "Too small! Try a larger one~"
  10.     elif guess > secret:
  11.         return "Too large! Try a smaller one~"
  12.     else:
  13.         return "Congratulations! You've found out the corresponding number!"

  14. attempts = 0
  15. max_attempts = 10

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

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

  23.     attempts += 1

  24.     result = check_guess(guess, secret_number)
  25.     print(result)

  26.     if guess == secret_number:
  27.         print(f"{attempts} times till you get the answer! Well done! ")
  28.         break

  29.     remaining = max_attempts - attempts
  30.     if remaining > 0:
  31.         print(f"{remaining} chances remaining~ \n")
  32.     else:
  33.         print("Just wasted all your chances! ")
  34.         print(f"The secret number is {secret_number}. Try again next time~ ")
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-8-20 23:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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