在學習Pthyon階段,在迭代輸出列表的時候,創建成了集合對象,出現了這個BUG(在創建對象時,將()寫成了{})
以下是錯誤案例:
#創建的集合 names = {"你大爺","你二大爺","你三大爺"} ages = {18,19,20,21} jobs = {"老師","程序員","打醬油的"} for i in range(3): print("姓名:{0},年齡:{1},工作:{2}".format(names[i],ages[i],jobs[i])) #TypeError: 'set' object is not subscriptable #表示把不具有下標操作的集合對象用成了對象[i]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
以下是正確案例:
names1 = ("你大爺","你二大爺","你三大爺") ages1 = (18,19,20,21) jobs1 = ("老師","程序員","打醬油的") for i in range(3): print("姓名:{0},年齡:{1},工作:{2}".format(names1[i],ages1[i],jobs1[i]))
- 1
- 2
- 3
- 4
- 5
- 6
新手上路,請多指教~