How do i add '$' when printing ljusted list of tuples

hi all, ive been struggling with this problem for a while now, any help is appreciated. the goal of this problem is to input a list of tuples and output a formatted string, see below:

item1 = 'Krusty Burger'
item2 = 'Milkshake'
item3 = 'Krusty Meal Set[Burger + Drink + Krusty Laugh]'
item1_cost = 5.10
item2_cost = 3.50
item3_cost = 10.50

menu_list = [(item1, item1_cost), (item2, item2_cost), (item3, item3_cost)]
expected output:
Krusty Burger                                    $ 5.10
Milkshake                                        $ 3.50
Krusty Meal Set[Burger + Drink + Krusty Laugh]   $10.50

how will i go about achieving this? i have tried this method but ljust wont work with float values (i must keep the item costs as floats)

def get_menu():
    i = 0
    j = 0
    while i < len(menu_list):
        while j < len(menu_list[i]):
            print(menu_list[i][j].ljust(49), end='')
            j += 1
        print()
        j = 0
        i += 1
    print()

‘output a formatted string’ suggests using one of the .format() method, or f-string which both allow for padding and justification.

In order to use ljust() string method on a float object, you must first convert the float object to a string. If you insert a float object into a string via string formatting, that will be taken care of automatically.

As suggested, you need to use string formatting. You can read about that here . On the same page are some examples.

May I give a hint how you can move forward? I know you need to use a while loop for this. With that in mind, my hint is that you can accomplish what you need with only one while loop and one print statement within that loop. You won’t need to use j.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.