python3 出现错误:TypeError: must be str, not list

def make_pizza(*toppings):
    for j in range(0,len(toppings)):
        print(str(j)  + toppings[j])
list1 = ['s1','s2','s3','s4']
list2 = ['s3','s45','s4','s5']
make_pizza(list1,list2)    
make_pizza('sdf','asdf','asdf','asdf')

运行结果出现错误如下:

r@r:~/python$ python3 1.py
Traceback (most recent call last):
  File "1.py", line 6, in <module>
    make_pizza(list1,list2)    
  File "1.py", line 3, in make_pizza
    print(str(j)  + toppings[j])
TypeError: must be str, not list

关键在于print() 括号内,用加号连接的必须是str类型。固必须把列表转化成str

一定是需要str的地方,出现了非str类型对象。

解决方法:强制转换成str

猜你喜欢

转载自blog.csdn.net/digitalkee/article/details/108428574