gpt4 book ai didi

Python错误: TypeError: '_csv.reader' object is not subscriptable

转载 作者:太空宇宙 更新时间:2023-11-03 17:27:19 24 4
gpt4 key购买 nike

我正在创建一个变化计算器,它必须输出与此类似的结果:

{'TWENTY':1, 'TEN':1, 'FIVE':1, 'PENNY’ : 2}
{‘ONE’:1,'FIVE':1}
{‘ONE’:1}
{'PENNY':4,'NICKEL':1}

我在读取数据和访问索引时遇到问题,因为我以前从未读取过 .csv 文件。我想我的计算器逻辑应该没问题。我意识到有更有效的方法来编码,但我有时间限制,这对我来说似乎是合乎逻辑的。如有任何帮助,我们将不胜感激。

谢谢

enter code here__author__ = 'austincolaizzi'

import csv

print("Enter in your file name: ")

file1 = input(" ")

txt1 = open(file1)


csv_txt1 = csv.reader(txt1)

#for row in csv_txt1:
#print (row[0:2])


difference = 0
remainder = 0
change = {
'Penny': 0,
'Nickel': 0,
'Dime': 0,
'Quarter': 0,
'Half Dollar': 0,
'One': 0,
'Two': 0,
'Five': 0,
'Ten': 0,
'Twenty': 0,
'Fifty': 0,
'Hundred': 0,
}

for row in csv_txt1:
if row[0] == row[1]:
print ("You had exact change")
elif row[0] > row[1]:
print ("You need more money to purchase this")
else:
difference = csv_txt1[1] - csv_txt1[0]
if difference % 100 == 0:
remainder = remainder
elif difference % 100 != 0:
change['Hundred'] = difference / 100
remainder = difference % 100
elif remainder % 50 == 0:
remainder = remainder
elif remainder % 50 != 0:
change['Fifty'] = remainder / 50
remainder = remainder % 50
elif remainder % 20 == 0:
remainder = remainder
elif remainder % 20 != 0:
change['Twenty'] = remainder / 20
remainder = remainder % 20
elif remainder % 10 == 0:
remainder = remainder
elif remainder % 10 != 0:
change['Ten'] = remainder / 10
remainder = remainder % 10
elif remainder % 5 == 0:
remainder = remainder
elif remainder % 5 != 0:
change['Five'] = remainder / 5
remainder = remainder % 5
elif remainder % 2 == 0:
remainder = remainder
elif remainder %2 != 0:
change['Two'] = remainder / 2
remainder = remainder % 2
elif remainder % 1 == 0:
remainder = remainder
elif remainder %1 != 0:
change['Two'] = remainder / 1
remainder = remainder % 1
elif remainder % int(.50) == 0:
remainder = remainder
elif remainder % int(.50) != 0:
change['Two'] = remainder / int(.50)
remainder = remainder % int(.50)
elif remainder % int(.25) == 0:
remainder = remainder
elif remainder % int(.25) != 0:
change['Two'] = remainder / int(.25)
remainder = remainder % int(.25)
elif remainder % int(.10) == 0:
remainder = remainder
elif remainder % int(.10) != 0:
change['Two'] = remainder / int(.10)
remainder = remainder % int(.10)
elif remainder % int(.05) == 0:
remainder = remainder
elif remainder % int(.05) != 0:
change['Two'] = remainder / int(.05)
remainder = remainder % int(.05)
else:
remainder = remainder / int(.01)


for key in change:
if change[key] >= 1:
print (change[key])

最佳答案

在 else 语句之后的行中,您有 csv_txt1[1] - csv_txt1[0]

这应该是 row[1] - row[0],如您之前所做的那样。

请注意,您应该发布回溯,这将使调试变得更容易,因为它显示了发生错误的确切行。

关于Python错误: TypeError: '_csv.reader' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com