Python 嵌套的 if-else 语句 [英] Python nested if-else statements

查看:72
本文介绍了Python 嵌套的 if-else 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的声明

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 块(if word in new_words),这可能吗?我在识别方面做错了吗?

And I am very suspicious that the else block is executed every time when the first condition is not met (if word in new_words), is this possible?Am I am doing something wrong with indentication?

推荐答案

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

The else clause corresponds to the if on the same level of indentation, as you expect.

您看到的问题可能是由于您混用了制表符和空格,因此明显的缩进程度与您的解释器看到的不同.

The problem you see may be due to the fact that you are mixing tabs and spaces, so the apparent level of indentation is not the same as the one your interpreter sees.

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

Change all tabs into spaces and check if the problem goes away.

这篇关于Python 嵌套的 if-else 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆