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

如何利用字符串变量引用字典中字符串类型的键?——战斗系统弱点判断代码报错排查

Fixing the "NoneType is not iterable" Error in Your Combat System Weakness Check

Hey there, let's break down why you're hitting that TypeError and fix it step by step.

The Root Cause

The error happens because weakness_dict.get(defender) is returning None. That means the defender value you're passing to check_weakness() doesn't exist as a key in your weakness_dict dictionary. When you try to run if attack in defweakness, you're trying to check membership in a None value—hence the "not iterable" error.

Common Reasons & Fixes

Let's go through the most likely issues and how to fix them:

1. Mismatched Keys (Spaces/Extra Characters)

Looking at your weakness_load() function, when you split lines with i.split(","), there might be hidden spaces in your input file. For example, if a line is written as Rock , Fighting;Steel;Ground;Water;Grass;, splitting it would give you a basetype of "Rock " (with a trailing space) instead of "Rock". When you later pass "Rock" as defender, the keys won't match.

Fix: Add .strip() to clean up whitespace from your split values:

def weakness_load(): 
    global weakness_dict  # Don't forget this if weakness_dict is global!
    pokeweakness.seek(0) 
    dump = pokeweakness.read().split("\n") 
    for i in dump: 
        print(i) 
        cleaned_line = i.strip()
        if cleaned_line != "": 
            basetype, allbaseweaknesses = cleaned_line.split(",") 
            # Strip whitespace from both values
            basetype = basetype.strip()
            allbaseweaknesses = allbaseweaknesses.strip()
            print(basetype, allbaseweaknesses) 
            weakness_dict[basetype] = allbaseweaknesses 
            print(weakness_dict[basetype]) 
            print("\n")

2. weakness_dict Isn't Accessible Globally

If you defined weakness_dict outside the weakness_load() function, but didn't declare it as global inside the function, you're actually creating a local dictionary that gets discarded after the function runs. The check_weakness() function would then be using an empty or unpopulated dictionary.

Fix: Add global weakness_dict at the top of weakness_load() (as shown in the code above) to ensure you're modifying the global dictionary.

3. Handle Missing Keys Gracefully

Even after fixing the loading issue, it's good practice to handle cases where a defender type doesn't exist in your dictionary (to avoid crashes). Update your check_weakness() function to check for None first:

def check_weakness(attack, defender, mover): 
    defweakness = weakness_dict.get(defender) 
    print(defweakness) 

    # Handle missing defender type
    if defweakness is None:
        print(f"Warning: No weakness data found for defender type '{defender}'")
        return False  # Return a default value that makes sense for your system

    if attack in defweakness: 
        if mover == 1: 
            return True  # Simplified: no need for extra variables
        elif mover == 2: 
            return True
    # Return False if no weakness is found
    return False

4. Debug to Verify Keys

If you're still stuck, add a debug print in check_weakness() to see exactly what keys are in your dictionary:

print("Available weakness keys:", list(weakness_dict.keys()))

This will let you compare the defender value you're passing against the actual keys in the dictionary—you might spot mismatches like capitalization (e.g., "rock" vs "Rock") or typos.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 17:17:32