if/elif/else python中的缩进错误

2024-06-28 19:49:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在语句的其他部分得到缩进错误。我已经检查了它的空白处,无法解释错误。

if cur_state == 'NICHTGEN':
    cur_state = 'GEN'
elif cur_state == 'GEN' and chance_num > trans_genZuGen:
    cur_state = 'NICHTGEN'
else:
    cur_state = 'GEN'

准确的错误是

    else: 
        ^ 
IndentationError: unindent does not match any outer indentation level

Tags: andtransif错误语句elsenumgen
3条回答

让空闲处理间距。在每行上使用“结束”键、“删除”键和“返回”键。

确保不要同时使用空白和制表法缩进。

另请参见:Are there any pitfalls with using whitespace in Python?

首先-你可能是混合空间和标签。

第二,这个逻辑可以简化为

if cur_state == 'GEN' and chance_num > trans_genZuGen:
    cur_state = 'NICHTGEN'
else:
    cur_state = 'GEN'

甚至

cur_state = 'NICHTGEN' if cur_state == 'GEN' and chance_num > trans_genZuGen else 'GEN'

(尽管第一个确实更具可读性。)

相关问题 更多 >