Reputation: 17
I have a problem.
I want to display all my records in "view" command, but it only displays the last record.
How do I solve the problem?
Please help me , thank you!
my code:
initial_money = int(input('How much money do you have? '))
def records():
return
def add(records):
records = input('Add an expense or income record with description and amount:\n').split()
global rec
rec = records[0]
global amt
amt = records[1]
global initial_money
initial_money += int(amt)
def view(initial_money, records):
print("Here's your expense and income records:")
print("Description Amount")
print("------------------- ------")
print('{name:<20s} {number:<6s}'.format(name = rec,number = amt))
print('Now you have {} dollars.'.format(initial_money))
while True:
command = input('\nWhat do you want to do (add / view / delete / exit)? ')
if command == 'add':
records = add(records)
elif command == 'view':
view(initial_money, records)
Output
How much money do you have? 100
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
tomato -50
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
salary 100
What do you want to do (add / view / delete / exit)? view
Here's your expense and income records:
Description Amount
------------------- ------
salary 100
Now you have 150 dollars.
output I want:
------------------- ------
tomato -50
salary 100
Now you have 150 dollars.
Upvotes: 0
Views: 38
Reputation: 15498
I will use a dictionary to hold them instead:
initial_money = int(input('How much money do you have? '))
mr = {}
def records():
return
def add(records):
records = input('Add an expense or income record with description and amount:\n').split()
global rec
rec = records[0]
global amt
amt = records[1]
global mr
global initial_money
mr[rec] = int(amt)
initial_money += mr[rec]
def view(initial_money, records):
print("Here's your expense and income records:")
print("Description Amount")
print("------------------- ------")
for r,a in mr.items():
print('{name:<20s} {number:<6s}'.format(name = r,number = str(a)))
print('Now you have {} dollars.'.format(initial_money))
while True:
command = input('\nWhat do you want to do (add / view / delete / exit)? ')
if command == 'add':
records = add(records)
elif command == 'view':
view(initial_money, records)
Test:
How much money do you have? 100
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
tomato -50
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
salary 100
What do you want to do (add / view / delete / exit)? view
Here's your expense and income records:
Description Amount
------------------- ------
tomato -50
salary 100
Now you have 150 dollars.
Upvotes: 1
Reputation: 2358
You need to make records into a list of the records you inserted. Currently you are overwriting it each time you call add.
Also for bonus, stop using global in your add function, It's considered bad form
Upvotes: 1