0

The goal of this is to enter an amount and date for each account on a regular basis. The accounts are static (but more could be appended). What I am trying to do is loop through each account and for each account enter the amount and date. I am doing something wrong, I think in the increment line, and possibly the append line

After that I'd like to print the results to the screen in a way that makes sense (I know what I have is not correct and won't display in a sensible way)

Any ideas? Thanks

account = ['401k', 'RothIRA', 'HSA']
amount = []
date = []

while True:
    print('Enter the amount for your ' + account[0] + ' account')
    act = input()
    amount.append(act)
    print('Enter the date for this total')
    dt = input()
    date.append(dt)
    account[] += 1
    if act == '':
        break


print(account, amount, date)
7
  • Your solution is screaming for a dictionary. I strongly suggest you use a dictionary for crafting this. Commented Jul 22, 2017 at 3:29
  • No need for account[] += 1. When you do append list size is automatically incremented. Commented Jul 22, 2017 at 3:29
  • @idjaw thanks, I tried a dictionary but didn't get very far with that either Commented Jul 22, 2017 at 3:30
  • If your final solution is really this simple, read up on how to use Python Dictionaries. They are great for this kind of problem. If you need more functionality, consider defining your own object class for each account so that you don't need to be counting at the same time; that will give you more control over every account. Commented Jul 22, 2017 at 3:31
  • @ksai the account [] += 1 was meant for the hardcoded accounts, and looping through them while populating the other 2 lists Commented Jul 22, 2017 at 3:31

2 Answers 2

1

I think this is what you're trying to do:

i = 0

while i < len(account):
    print('Enter the amount for your ' + account[i] + ' account')
    act = input()
    amount.append(act)
    print('Enter the date for this total')
    dt = input()
    date.append(dt)
    i += 1

It's better to use for loop as follows.

for i in account:
    print ('Enter amount for your',i,'account')
    act = input()
    amount.append(act)
    print ('Enter date')
    dt = input()
    date.append(dt)

Also all your lists (account, amount, date) are linked by index. It's much cleaner to use dictionary as someone else posted.

Sign up to request clarification or add additional context in comments.

Comments

1

After a slight change in your data structure:

account={ '401K': { 'amount' : 0, 'date': 0 },
          'ROthIRA': { 'amount':0, 'date': 0},
          'HSA': { 'amount': 0, 'date': 0} }

for eachKey in account.keys:
    account[eachKey]['amount'] = input()
    account[eachKey]['date'] = input()


print account

1 Comment

Thank you both. Clearly I need to look into dictionaries to make this work better. Thank you both for the code examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.