Python基础问题

代码如图输出是这样的Enter a character or press the Return key to finish: hEnter a character or press the Return key to finish: 5为什么不是2啊
输出是这样的..

state_map = {
    1: {
        "h": 2,
        "default": 5
    },
    2: {
        "a": 3,
        "o": 3,
        "default": 5
    },
    3: {
        "h": 2,
        "!": 4,
        "default": 5
    },
    4: {
        "": 4,
        "default": 5
    }
}

def find_state(state, ch):
    if state not in state_map:
        raise Exception("Has no such state in state_map")
    try:
        state = state_map[state].get(ch)
    except Exception as e:
        state = state_map[state].get("default")
    return state
# if else elif 能简化就简化吧,提供一个思路,代码还没有执行过
温馨提示:答案为网友推荐,仅供参考
相似回答