Use a while loop to collect a shopping list of items from the user. Then use a while or for loop to list it back to them.

i have:

item = int(input('How many items do you have? '))
        while item  > 0:
            input('Name of item:  ')
            item -= 1

i don't know how to change this into a list, or repeat it with a loop

Recommended Answers

All 3 Replies

You use a Python list object this way ...

number_item = int(input('How many items do you have? '))

mylist = []  # start with an empty list
count = 1
while number_item  > 0:
    item = input('{}) Name of item:  '.format(count))
    mylist.append(item)
    number_item -= 1
    count += 1

# test
print(mylist)

''' possible result ...
How many items do you have? 3
1) Name of item:  hot dogs
2) Name of item:  buns
3) Name of item:  mustard
['hot dogs', 'buns', 'mustard']
'''

Once you have the list, you can show its contents this simple way ...

# show list items to customer
print("You ordered:")
mylist = ['hot dogs', 'buns', 'mustard']
for item in mylist:
    print(item)

''' result ...
You ordered:
hot dogs
buns
mustard
'''

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.