from art import logo import random from replit import clear
def guess_the_number(): # random.randint(a, b):返回 a–b 之间的随机整数,两端都包含。 # 所以这里是 1 到 100(含 100),不是 0 到 100。 guess_this = random.randint(1, 100) print("Welcome to: ") print(logo) # 调试时可以取消注释偷看答案——注释掉的那一行就是“开发时的小抄”。 # print(f"The number is {guess_this}") print("I'm thinking of a number between 1 and 100, try to guess it") difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ") if difficulty == 'easy': guesses = 10 elif difficulty == 'hard': guesses = 5 # 注意:如果用户输入了别的词,guesses 根本没被赋值, # 下一行 while guesses > 0 会抛 NameError(未定义变量)。
# 计数循环:guesses 既是“剩余次数”,又是循环条件,每轮末尾减 1。 while guesses > 0: if guesses > 1: print(f"You have {guesses} guesses left for the number that I'm thinking of.") else: print(f"Last try to guess the number that I'm thinking of.") # int():把输入转成整数才能和答案比较大小。 attempt = int(input("Take you guess: ")) if attempt > guess_this: print("Too high.") elif attempt < guess_this: print("Too low.") # 这里的顺序有问题:先判“是不是最后一次”,再判“猜对了没有”。 # 结果是——最后一次即使猜对,也会先打印 Game over,虽然接着还会打印 Correct。 # 正确顺序应当是:先判 attempt == guess_this,再判次数耗尽。 if guesses == 1: print("Game over.") elif attempt == guess_this: # 局内又定义了一个函数 game_over(),只干两件事:打印成功信息、把计数清零。 # return guesses - guesses 就是 return 0——用“相减”来表达 0,写法很绕, # 直接 return 0 更清楚。 def game_over(): print(f"Correct! The answer was {guess_this}. Thanks for completing that! 😁") return guesses - guesses # 把返回值 0 赋给 guesses,下一轮 guesses -= 1 变成 -1,while 条件自然结束。 guesses = game_over() guesses -= 1
play_again = input("nDo you want to play again? Type 'y' if yes and 'n' to quit.") if play_again == 'y': clear() # 递归重开:新一局有自己的 guess_this 和 guesses,互不干扰。 guess_the_number() else: print("Goodbye.")
I'm thinking of a number between 1 and 100, try to guess it Choose a difficulty. Type 'easy' or 'hard': hard You have 5 guesses left ... Take you guess: 50 Too high.
易错点
函数内改了全局变量却没 global:外面看不到变化。
UnboundLocalError:函数里既想读又想写同名全局变量,忘了 global。
递归重开导致状态残留:每次重开都新建局部变量才安全。
int(input()) 没做异常处理:输入字母直接崩。
动手改
修掉上面三个 bug。
把 guesses 改成常量 MAX_GUESSES。
每轮提示"你猜的范围是 [low, high]",用二分策略体验最快解法(7 次内必中)。
源文件逐个讲解
3 个文件:main.py(45 行)、art.py(7 行)、README.md(7 行)。
art.py(7 行)
一幅 GUESS THE NUMBER 的 ASCII 字,变量名 logo,纯素材。
main.py(45 行)
结构:一个递归函数 guess_the_number(),里面套一个 while 循环,循环里又临时定义一个 game_over()。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from art import logo import random from replit import clear
def guess_the_number(): guess_this = random.randint(1, 100) print("Welcome to: ") print(logo) # print(f"The number is {guess_this}") print("I'm thinking of a number between 1 and 100, try to guess it") difficulty = input("Choose a difficulty. Type 'easy' or 'hard': ") if difficulty == 'easy': guesses = 10 elif difficulty == 'hard': guesses = 5
while guesses > 0: if guesses > 1: print(f"You have {guesses} guesses left for the number that I'm thinking of.") else: print(f"Last try to guess the number that I'm thinking of.") attempt = int(input("Take you guess: ")) if attempt > guess_this: print("Too high.") elif attempt < guess_this: print("Too low.")
提示语在「最后一次」时会变,这是体验细节。int(input(...)) 没校验,输入非数字崩。
1 2 3 4 5 6 7 8
if guesses == 1: print("Game over.") elif attempt == guess_this: def game_over(): print(f"Correct! The answer was {guess_this}. Thanks for completing that! 😁") return guesses - guesses guesses = game_over() guesses -= 1
这一段是当天最严重的 bug:
if guesses == 1 在最前面,所以最后一次机会无论猜没猜中都先打印 "Game over."。猜中了也是「Game over」+ 不再判胜。
play_again = input("nDo you want to play again? Type 'y' if yes and 'n' to quit.") if play_again == 'y': clear() guess_the_number() else: print("Goodbye.")
递归重开:和 Day 10/11 一样。玩 30 局就 30 层栈。
这份代码的问题
最后一次机会必输(if guesses == 1 提前拦截)。
difficulty 非法时 guesses 未定义 → UnboundLocalError。
game_over() 在循环里定义、返回恒 0,纯噪音。
guesses -= 1 无条件执行,胜利后状态错乱。
int() 无校验。
递归重开。
README.md(7 行)
标题 Day 12 - Scope & Number Guessing Game,知识点是 Local Scope、Global Scope、Block Scope、Modify Global Variables、Global Constants。