简体   繁体   English

Python - 类型错误:+ 不支持的操作数类型:'NoneType' 和 'str'? 我究竟做错了什么?

[英]Python - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'? What am i doing wrong?

I am trying to create a Python definition to display a list and trying to add a feature where if the list is more than 10 it will display it horizontally.我正在尝试创建一个 Python 定义来显示一个列表,并尝试添加一个功能,如果列表超过 10 个,它将水平显示它。

Here's my code:这是我的代码:

def print_vert_list(list):
index = 0
for i in list:
    if len(list) > 10:
        print (" ".join(list[index]) + " ".join(list[11:11+index])) + " ".join(list[21:21+index])
    else:
        print (" ".join(list[index]))
        index += 1

And here's the log:这是日志:

Traceback (most recent call last):
File "**********", line 30, in <module>
print_vert_list(file_var_list)
File "**********", line 22, in print_vert_list
print (" ".join(list[index]) + " ".join(list[11:11+index])) + " ".join(list[21:21+index])
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

You must have an element contained in the list where it is not a string.您必须在列表中包含一个不是字符串的元素。 The error message is telling you that the program is breaking because you are trying to concatenate a None type with a string type.错误消息告诉您程序正在中断,因为您正在尝试将 None 类型与字符串类型连接起来。 For example:例如:

a = "string1" + "string2" print(a) # will give you string1string2

But if you try a = None + "string1" # You will get the same error message但是如果你尝试a = None + "string1" # You will get the same error message

Using this logic, you can add a conditional check to make sure that the element of the list is not of None type, then you could successfully concatenate it使用此逻辑,您可以添加条件检查以确保列表的元素不是 None 类型,然后您就可以成功连接它

print() returns None . print()返回None You cannot add a string to the result of a call to print as you cannot add None and a string您不能在调用 print 的结果中添加字符串,因为您不能添加None和字符串

print(" ".join(list[index]) + " ".join(list[11:11+index])) + " ".join(list[21:21+index])
                                   end of the print call ^ ^ You cannot add a string here

The + operator is only defined for strings (...and numbers however in another context...) +运算符只为字符串定义(...和数字但是在另一个上下文...)

In line print (" ".join(list[index]) + " ".join(list[11:11+index])--->>>)<<<--- + " ".join(list[21:21+index]) is a wrong parenthesis.行内print (" ".join(list[index]) + " ".join(list[11:11+index])--->>>)<<<--- + " ".join(list[21:21+index])是一个错误的括号。 Print function returns NoneType and because of this additonal parenthesis the intepreter thinks that the print statement ends here. Print 函数返回NoneType并且由于这个附加括号,解释器认为打印语句到此结束。 So this additional parenthesis causes the same behavior like described in Python 3.3 TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' and so throws the same error所以这个额外的括号会导致与Python 3.3 TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' 中描述的相同的行为,因此抛出相同的错误

If I understand correctly the question then this is a solution:如果我正确理解了这个问题,那么这是一个解决方案:

def print_vert_list(list_items):
    if len(list_items) > 10:
        for i in list_items:
            print(i, end=' ')
    else:
        for i in list_items:
            print(i)

Your parentheses are wrong in that line.你的括号在那一行是错误的。 You would want:你会想要:

print (" ".join(list[index]) + " ".join(list[11:11+index]) + " ".join(list[21:21+index]))

instead of:代替:

print (" ".join(list[index]) + " ".join(list[11:11+index])) + " ".join(list[21:21+index])

which gives you an error because you try to add a string to the return value of print .这会给您一个错误,因为您尝试将字符串添加到print的返回值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我究竟做错了什么? TypeError: 不支持的操作数类型 -: 'str' 和 'Cube' - What am I doing wrong? TypeError: unsupported operand type(s) for -: 'str' and 'Cube' 我有一个 TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' - I am having a TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:+不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:&:不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for &: 'NoneType' and 'str' 类型错误:+ 不支持的操作数类型:“NoneType”和“str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' 类型错误:+= 不支持的操作数类型:&#39;NoneType&#39; 和 &#39;str&#39; - TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str' TypeError:%支持的操作数类型:&#39;NoneType&#39;和&#39;str&#39; - TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' Python错误-TypeError:+:&#39;NoneType&#39;和&#39;str&#39;不支持的操作数类型 - Python error - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:+的不支持的操作数类型:在执行python脚本时为“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' in executing python script Python TypeError:+不支持的操作数类型:&#39;NoneType&#39;和&#39;str&#39; - Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM