Python 嵌套的 if-else 语句

标签 python python-2.7

我有这样的声明

for word in tweet_text:
    if word in new_words:
        if new_words[word] == 0:
             new_words[word] = sent_count
        else:
             new_words[word] = (new_words[word] + sent_count) / 2

而且我非常怀疑每次不满足第一个条件时都会执行 else block (if word in new_words),这可能吗?我是不是在缩进方面做错了什么?

最佳答案

如您所料,else 子句对应于同一缩进级别的 if

您看到的问题可能是由于您混用了制表符和空格,因此缩进的表观级别与您的解释器看到的不同。

将所有制表符更改为空格并检查问题是否消失。

关于Python 嵌套的 if-else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16449634/

相关文章:

python - 在 ipython 中执行 `ls` 时如何修复 PermissionError?

python - 如何使两个 LabelFrame 彼此对齐?

python - 尝试从目录外部导入时出现 ModuleNotFoundError

python - Win32api 没有在 python 中使用 GetCursorPos() 给出正确的坐标

python - 如何正确报告 Docker 容器内的可用 RAM?

python - 如何通过套接字检索 python.logging 日志记录

python - 在python中追加错误

Python argparse : Does it have to return a list?

python - 使用堆栈反转Python中的单词顺序

algorithm - 什么抽象数据类型 (ADT) 用于在 Python 中实现 steinhaus-johnson-trotter(排列)算法?