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','as
·
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
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献17条内容
所有评论(0)