Python错误:TypeError:' _csv.reader'对象不可订阅

时间:2015-09-03 22:25:05

标签: python csv dictionary calculator

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

{'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])

1 个答案:

答案 0 :(得分:1)

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

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

请注意,您应该发布回溯,这样可以更容易调试,因为它显示错误发生的确切行。