Need help with lists, tuples and 'for', 'in' tools

hi all im new to python, just need help with this

tup0 = ('hi', '')
tup1 = ('hi1', '$ 1')
tup2 = ('hi2', '$ 2')
tup3 = ('hi3', '$30')

for i in ls:
    for j in i:
        print(entry.ljust(49), end='')

how can i change the code from where i marked so that i dont use ‘for’ and ‘in’ while producing the same output? any help is appreciated

what do you want to use if not a for loop?

a while loop if possible

what is ls? that is not visible in your code

ls = [tup0, tup1, tup2, tup3]

what are the characteristics of a while loop? what do you need to add to a while loop so that iterates over a list?

its just a restriction, i cannot use for loops, only while loops

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)]

def get_menu():
    print('Welcome to Krusty Burgers!')

    # for items in menu_list:
    #     for test in items:
    #         print(test.ljust(49), end='')
    #     print()

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


what i commented out works as intended, how can i make it so using while loops it produces the same output, u can see my attempt above

these written outside the loop, their value will never update. Maybe you want to put them where they can be updated?

You may look into using a for loop for this. Those two while loops are doing things that python can do for you.

Did you know that you can unpack a tuple into variables while iterating over a list of tuples in a for loop?

>>> list_of_tuples = [("one", "two")]
>>> for a, b in list_of_tuples:
...     print(a, b)
one two

If you intended for this output it can be done with a for loop and just one print statement after that:

Welcome to Krusty Burgers!
Krusty Burger                                     $ 5.10
Milkshake                                         $ 3.50
Krusty Meal Set[Burger + Drink + Krusty Laugh]    $ 10.50

as i said, im not allowed to use for loops, they are restricted in the question im doing. hence i need to use while loops or another method

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

ive changed the code so that the tuple loop is now working, but the main loop to cycle through the list isnt unfortunately

SOLVED: check new thread for different problem

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