Python 3.9下If语句逻辑修复:游戏段位判定输出调试
修复你的段位判断Python代码
原代码存在的问题
- 函数参数误用:定义
rank_output(lowelo)但实际传入rank,参数名与逻辑不匹配,且未正确接收要判断的段位值 - 成员判断逻辑错误:用
==直接比较字符串和列表永远不成立,需用in关键字检查段位是否在对应列表中 - 段位拼写错误:
Ascendand应为Valorant官方正确拼写Ascendant - 高段位输出不符合需求:原输出"good job",未匹配"sheesh"或"nice"类内容的要求
- 缺少异常处理:未考虑输入段位不在两个列表中的场景
修复后的代码
lowelo = ["Iron", "Bronze", "Silver", "Gold", "Plat"] highelo = ["Diamond", "Ascendant", "Immortal", "Radiant"] name = input("whats ur name: ") fav_game = input("what is your favourite game?: ") # 针对Valorant设计 rank = input("What is your rank in the game?: ").strip() def rank_output(user_rank, user_name): if user_rank in lowelo: print(f"you are trash {user_name}") elif user_rank in highelo: # 随机输出需求指定的内容 import random print(random.choice(["sheesh", "nice", "solid play"]), user_name) else: print(f"Sorry, we don't recognize the rank {user_rank}") rank_output(rank, name)
修复说明
- 函数参数优化:改为接收
user_rank和user_name,避免依赖全局变量,代码更健壮 - 修正核心判断逻辑:用
in关键字检查段位是否在目标列表中,确保判断有效 - 修正拼写错误:将
Ascendand改为正确的Ascendant,保证段位匹配准确 - 满足高段位输出需求:用
random.choice随机输出"sheesh""nice"类内容,符合要求 - 添加异常处理:新增
else分支处理未知段位的情况,提升用户体验 - 输入预处理:对
rank调用.strip()去除首尾空格,避免因输入空格导致匹配失败
内容的提问来源于stack exchange,提问作者sewshyi
相关产品推荐
相关产品推荐

