python错误总结

《1》错误形式:

    if number %2 == 1:
       ~~~~~~~^~
TypeError: unsupported operand type(s) for %: 'list' and 'int'

"i"是在循环过程中代表当前列表元素的变量。在每次迭代中,"i"都会被设置为列表中的下一个元素。
在这里插入图片描述
在Python中,list对象没有模运算符,因为它不是一个整数。因此,你应该使用for循环迭代列表中的每个元素,并根据元素的值执行操作

a=[1,2,3,4,5]
print(type(a))
print(type(a[0]))

解决办法:
(1) if number[i] %2 == 1:
(2) if i % 2 == 1:

《2》TypeError: print() argument after * must be an iterable, not int
意味着您在 print 语句后使用了 运算符并跟了一个非可迭代值,换言之,您试图在期望迭代对象时解包非迭代对象。

*在Python中被称为unpacking操作符,用于将一个可迭代对象(如列表、元组等)中的元素解包为单独的元素。

在这里插入图片描述

《3》

    print(**a)
TypeError: print() argument after ** must be a mapping, not set

在这里插入图片描述
《4》


x='1234'
print(type(*x))

在这里插入图片描述
<5>

a, *b, *c = [1, 2, 3, 4, 5]  # SyntaxError: two starred expressions in assignment

<6> type.new() argument 2 must be tuple, not str
<7>TypeError: can’t multiply sequence by non-int of type ‘type’

猜你喜欢

转载自blog.csdn.net/m0_74154295/article/details/130548159