python3 出现错误:TypeError: must be str, not list
生活随笔
收集整理的這篇文章主要介紹了
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_pizzaprint(str(j) + toppings[j])
TypeError: must be str, not list
關鍵在于print() 括號內,用加號連接的必須是str類型。固必須把列表轉化成str
一定是需要str的地方,出現了非str類型對象。
解決方法:強制轉換成str
總結
以上是生活随笔為你收集整理的python3 出现错误:TypeError: must be str, not list的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ stringstream 实现字
- 下一篇: 压缩命令tar cf xx.tar -C