from art import logo, vs # 从 game_data.py 导入 data:它是 50 条字典组成的列表, # 每条形如 {"name": "Instagram", "follower_count": 346, "description": "…", "country": "…"} from game_data import data from replit import clear import random
# 随机取一条名人资料,把“描述文字”和“粉丝数”一起交回去。 # 无参数。返回值:一个元组 (compare, follower_count) # compare——拼好的介绍串,例如 "Instagram, a Social media platform, from United States." # follower_count——粉丝数(单位百万,int) def profile(): # data 有 50 条(下标 0–49),所以 randint(0, 49);randint 两端都包含。 random_number = random.randint(0, 49) # 先用下标取出那一条字典,再用键取值——两步可以合成一步:data[random_number]['name'] name = data[random_number]['name'] follower_count = data[random_number]['follower_count'] description = data[random_number]['description'] country = data[random_number]['country'] compare = (f"{name}, a {description}, from {country}.") # return 多个值,本质是返回一个元组;调用方可以用 a, b = profile() 一次接住。 return compare, follower_count
while correct: clear() print(logo) if score > 0: print(f"Correct! Your current score is: {score}.") # 下面这行是调试用的“偷看答案”,注释掉了;取消注释就能看到两边粉丝数。 # print (followers_a, followers_b) print(f"Compare A: {profile_a}") print(vs) print(f"Compare B: {profile_b}")
a_or_b = input("nWho do you think has more followers? Type 'a', or 'b': ").lower() # 四个分支穷举了“猜 A / 猜 B × 猜对 / 猜错”。 # 猜对时:得分 +1,并把“被淘汰的那一位”换成新抽的一位—— # 猜 A 对 → B 换成新人物,A 留在原位继续比;猜 B 对则相反。 if a_or_b == 'a' and followers_a >= followers_b: score += 1 profile_b, followers_b = profile() elif a_or_b == 'b' and followers_a <= followers_b: score += 1 profile_a, followers_a = profile() elif a_or_b == 'a' and followers_a < followers_b: print(f"nIncorrect, your final score is {score}.") correct = False elif a_or_b == 'b' and followers_a > followers_b: print(f"nIncorrect, your final score is {score}.") correct = False # 若输入不是 a/b(比如乱敲),四个分支全不命中,循环会原地重来一遍,不扣分。 play_again = input("nDo you want to play again? Type 'y' or 'n': ") if play_again == 'y': higher_lower() elif play_again == 'n': print("Thanks for playing!")
# 入口:定义完立刻开一局。 higher_lower()
代码走读
profile():随机取一条,拼出一句介绍(f"{name}, a {description}, from {country}."),返回(介绍文本, 粉丝数)。
while correct: clear() print(logo) if score > 0: print(f"Correct! Your current score is: {score}.") print(f"Compare A: {profile_a}") print(vs) print(f"Compare B: {profile_b}")
a_or_b = input("nWho do you think has more followers? Type 'a', or 'b': ").lower() if a_or_b == 'a' and followers_a >= followers_b: score += 1 profile_b, followers_b = profile() elif a_or_b == 'b' and followers_a <= followers_b: score += 1 profile_a, followers_a = profile() elif a_or_b == 'a' and followers_a < followers_b: print(f"nIncorrect, your final score is {score}.") correct = False elif a_or_b == 'b' and followers_a > followers_b: print(f"nIncorrect, your final score is {score}.") correct = False
核心的「接龙」逻辑:
猜 A 对 → B 换新人,A 留着(下一轮 A 就是上一轮的 B);
猜 B 对 → A 换新人,B 留着。
这就保证了「上一轮的赢家留在屏幕上」,游戏有连续性。这个设计比「每轮都换两个」高明。
四个分支用了 >= 和 <=,意味着粉丝数相等时选哪边都算对。但因为开局挡掉了相等,实际跑不到。
四个分支的 elif 没有兜底:如果用户输入 "c" 或直接回车,四个条件全不成立,循环体结束回到 while correct——屏幕上什么都不变,玩家会以为程序卡死了。这是这份代码最影响体验的地方,应该加:
1 2
else: print("只能输入 a 或 b")
1 2 3 4 5 6
play_again = input("nDo you want to play again? Type 'y' or 'n': ") if play_again == 'y': higher_lower() elif play_again == 'n': print("Thanks for playing!") higher_lower()
递归重开(第三次出现这个模式),最后一行顶格调用。
这份代码的问题
randint(0, 49) 硬编码数据条数。
输入非 a/b 时静默卡住(无 else 兜底)。
可能抽到同一条记录(靠「粉丝数相等就重抽」碰巧挡住)。
>= / <= 让平局算对,语义上应该是「必须严格大于」。
from replit import clear 本地跑不了。
递归重开。
README.md(8 行)
标题 Day 14 - Higher Lower Game Project,知识点是 Functions、Randomisation、Dictionaries、Lists、User Input、Score Keeping、Formatting。