You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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)

修复说明

  1. 函数参数优化:改为接收user_rank和user_name,避免依赖全局变量,代码更健壮
  2. 修正核心判断逻辑:用in关键字检查段位是否在目标列表中,确保判断有效
  3. 修正拼写错误:将Ascendand改为正确的Ascendant,保证段位匹配准确
  4. 满足高段位输出需求:用random.choice随机输出"sheesh""nice"类内容,符合要求
  5. 添加异常处理:新增else分支处理未知段位的情况,提升用户体验
  6. 输入预处理:对rank调用.strip()去除首尾空格,避免因输入空格导致匹配失败

内容的提问来源于stack exchange,提问作者sewshyi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 23:49:59