Reputation: 3801
I have what seems to be a simple problem but I can't get it correct. What I want to do is enter a Date and then a balance for each of the accounts listed below in the dictionary. After that I want to SUM the total. Seems simple enough but I am making a mess of it. Any ideas how to make this work? I know I want to use a Dictionary for this, as I intend to add onto this as I go along (add more accounts, or another key/value pair etc).
Thanks!
savings = {'month': '' ['HSA': 0, 'BofA': 0, 'RothIRA': 0]}
for item in savings:
month = input('Enter current month: ')
balance = int(input('Enter balance for: '{savings}))
savings[month][savings] = balance
print(savings)
print('Total savings for the month is: 'sum(balance) + 'dollars')
Upvotes: 1
Views: 431
Reputation: 2011
The better way may be is to go with a class as mentioned. However, to cater the simple needs, here goes
savings = {'month': {'HSA':0, 'BofA':0, 'RothIRA':0}};
for item in savings:
month = input("Enter current month: ");
monat = savings.get(month);
accNames = list(monat.keys());
print(accNames);
for s in range(len(accNames)):
balance = int(input("Enter balance for "+accNames[s]+" : "));
savings[month][accNames[s]] = balance;
print(savings[month]);
print("Total is :" + str(int(savings[month][accNames[0]]+savings[month][accNames[1]]+savings[month][accNames[2]])) + "$");
While the implementation can well be improved further, Hope this helps.
Upvotes: 1