Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Erase the first word in a line in a nested loop

I want to produce the following output:

20 $ 476321.30  $ 923268.18  $ 1859015.31  $ 3840898.15

with this code:

for age in range(20, 70, 5):
    for percentage in range(4, 12, 2):
        result = calc_final_balance(age, amount_saved, percentage)
        print(age, "\t $" + format(result, '.2f').rjust(10), end="")
    print()

but the problem is, the age is always printed after the amount:

20 $ 476321.3020  $ 923268.1820  $ 1859015.3120  $ 3840898.15
like image 953
natasha Avatar asked May 12 '26 08:05

natasha


1 Answers

You can easily do this. Just print it in your first, "higher" loop:

for age in range(20, 70, 5):
    print(age, end=" ") #end is space
    for percentage in range(4, 12, 2):
        result = calc_final_balance(age, amount_saved, percentage)
        print("\t $" + format(result, '.2f').rjust(10), end=" ")
    print()

This way it will produce something like:

20 $ 476321.30  $ 923268.18  $ 1859015.31  $ 3840898.15
70 $ someamount $ someamount $ someamount $ someamount
5 $ someamount $ someamount $ someamount $ someamount
like image 117
aIKid Avatar answered May 13 '26 21:05

aIKid