0
我正在使用函數來編碼購物清單。用戶被問到諸如對象的名稱,數量,他們將從中購買商店以及它的價格等問題。這些然後被添加到一個csv文件。用戶可以詢問總價,以便他們知道他們將花多少錢。TypeError:列表索引必須是整數,而不是功能元組
這裏是我的代碼:
def TotalCost():
ViewItem = ViewList()
with open ("C:\\Users\\sophie\\Documents\\Sophie\\Homework\\Year 11\\Computer Science\\ShoppingList.csv") as csvfile:
readcsv = csv.reader(csvfile)#delimeter=',')
TotalCost=0
for i in range(1,3):
TotalCost=TotalCost+int(ViewItem[i,3])
def ViewList():
with open ("C:\\Users\\sophie\\Documents\\Sophie\\Homework\\Year 11\\Computer Science\\ShoppingList.csv") as csvfile:
reader=csv.reader(csvfile)#,delimeter=',')
for row in reader:
ItemView.append(row)
return ItemView
下面是其他代碼correspomds的問題:
elif ModeChose=='C':
TotalCost()
這是錯誤我得到:
Traceback (most recent call last):
File "C:\Users\sophie\Documents\Sophie\Homework\Year 11\Computer Science\ShoppingList.py", line 106, in <module>
TotalCost()
File "C:\Users\sophie\Documents\Sophie\Homework\Year 11\Computer Science\ShoppingList.py", line 18, in TotalCost
TotalCost=TotalCost+int(ViewItem[i,3])
TypeError: list indices must be integers, not tuple
'ViewItem [i] [3]' – Barmar
你有一個名爲TotalCost()的函數和一個名爲TotalCost的變量... –
@ReblochonMasque我猜他只調用'TotalCost()'一次,所以它從來沒有注意到運行後他不能再調用它。 – Barmar