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

Python条件判断失效:输入任意内容均输出固定结果求助

问题根源与修复方案

核心问题1:逻辑表达式的布尔值判断错误

你的代码里所有if/elif条件都犯了同一个逻辑错误:比如第一个条件中的'shai' or 'Shai Gilgeous-Alexander' or 'SGA' in p.lower().strip(),在Python中非空字符串会被视为布尔值True,'shai'本身就是True,不管后面的in判断结果如何,整个条件都会直接成立,导致第一个if分支永远被触发,后续分支和else完全不会执行。

正确写法是对每个关键词单独做in判断,并用括号明确分组(因为and优先级高于or)。

核心问题2:方法调用遗漏括号

多处出现s.lower().strip,strip是字符串方法,必须加括号调用(strip()),否则只是引用方法对象,不会执行去除空格的操作。

修复后的代码

先将输入预处理为干净的字符串(转小写+去首尾空格),再简化逻辑判断:

def main():
    player_name = input("Which player on OKC Thunder's starting 5 is stats needed for?: ")
    stats_name = input("Which stat is needed for said player? (or say \"stats\" for all stats): ")
    okc_starting_5(player_name, stats_name)


def okc_starting_5(p, s):
    # 预处理输入:转小写+去除首尾空格
    p_cleaned = p.lower().strip()
    s_cleaned = s.lower().strip()
    
    # 提前判断球员身份,简化后续条件
    is_shai = 'shai' in p_cleaned or 'sga' in p_cleaned or 'shai gilgeous-alexander' in p_cleaned
    is_josh = 'josh' in p_cleaned or 'giddey' in p_cleaned or 'josh giddey' in p_cleaned
    
    if is_shai and ('points' in s_cleaned or 'pts' in s_cleaned):
        print("Shai averaged 31.4 points per game in the 2022-23 NBA Regular Season")
    elif is_shai and ('rebounds' in s_cleaned or 'reb' in s_cleaned):
        print("Shai averaged 4.8 rebounds per game in the 2022-23 NBA Regular Season")
    elif is_shai and ('assists' in s_cleaned or 'ast' in s_cleaned):
        print("Shai averaged 5.5 assists per game in the 2022-23 NBA Regular Season")
    elif is_shai and 'stats' in s_cleaned:
        print("Shai averaged 31.4 points, 4.8 rebounds and 5.5 assists in the 2022-23 NBA Regular Season")
    elif is_josh and ('points' in s_cleaned or 'pts' in s_cleaned):
        print("Josh averaged 16.6 points per game in the 2022-23 NBA Regular Season")
    elif is_josh and ('rebounds' in s_cleaned or 'reb' in s_cleaned):
        print("Josh averaged 7.9 rebounds per game in the 2022-23 NBA Regular Season")
    elif is_josh and ('assists' in s_cleaned or 'ast' in s_cleaned):
        print("Josh averaged 6.2 assists per game in the 2022-23 NBA Regular Season")
    elif is_josh and 'stats' in s_cleaned:
        print("Josh averaged 16.6 points, 7.9 rebounds and 6.2 assists in the 2022-23 NBA Regular Season")
    else:
        print("I cannot determine which statistics you need with this information. Please try again. ")

        
main()

额外优化建议

用字典存储球员数据,让代码更简洁易维护,后续添加球员或数据类型只需修改字典:

def main():
    player_name = input("Which player on OKC Thunder's starting 5 is stats needed for?: ")
    stats_name = input("Which stat is needed for said player? (or say \"stats\" for all stats): ")
    okc_starting_5(player_name, stats_name)


def okc_starting_5(p, s):
    p_cleaned = p.lower().strip()
    s_cleaned = s.lower().strip()
    
    # 字典存储球员别名与数据,便于扩展
    player_stats = {
        'shai': {
            'aliases': ['shai', 'sga', 'shai gilgeous-alexander'],
            'points': 31.4,
            'rebounds': 4.8,
            'assists': 5.5
        },
        'josh': {
            'aliases': ['josh', 'giddey', 'josh giddey'],
            'points': 16.6,
            'rebounds': 7.9,
            'assists': 6.2
        }
    }
    
    # 匹配目标球员
    target_player = None
    player_name = ""
    for name, data in player_stats.items():
        if any(alias in p_cleaned for alias in data['aliases']):
            target_player = data
            player_name = name
            break
    
    if not target_player:
        print("I cannot determine which statistics you need with this information. Please try again. ")
        return
    
    # 匹配目标数据
    if 'points' in s_cleaned or 'pts' in s_cleaned:
        print(f"{player_name.title()} averaged {target_player['points']} points per game in the 2022-23 NBA Regular Season")
    elif 'rebounds' in s_cleaned or 'reb' in s_cleaned:
        print(f"{player_name.title()} averaged {target_player['rebounds']} rebounds per game in the 2022-23 NBA Regular Season")
    elif 'assists' in s_cleaned or 'ast' in s_cleaned:
        print(f"{player_name.title()} averaged {target_player['assists']} assists per game in the 2022-23 NBA Regular Season")
    elif 'stats' in s_cleaned:
        print(f"{player_name.title()} averaged {target_player['points']} points, {target_player['rebounds']} rebounds and {target_player['assists']} assists in the 2022-23 NBA Regular Season")
    else:
        print("I cannot determine which statistics you need with this information. Please try again. ")

        
main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 05:08:09