如何用python计算一行中的列数

Glo*_*oom 0 python

我正在编写一个python代码,我试图找出哪个函数最好执行以下任务:

我想说"如果一行中的单词数不等于1,那么就做"

#Some code
words = line.split("\t")
if sum(words) != 1
#Do something

#Some code
words = line.split("\t")
if int(words) != 1
#Do something

#Some code
words = line.split("\t")
if len(words) != 1
#Do something
Run Code Online (Sandbox Code Playgroud)

所有这些命令都返回以下错误:

File "test.py", line 10
if len(words) != 1
                 ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Gui*_*ume 6

您收到此错误是因为您在每个"if"语句后都缺少冒号.另外,要获取单词中元素的数量,应使用len()而不是sum().

这个

if sum(words) != 1
#Do something
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

if len(words) != 1:
#Do something
Run Code Online (Sandbox Code Playgroud)