Python

Solution - Lists and Dictionaries

Shopping List

Our Solution

Here is our solution...

shopping_list = []

while True:
    print("") #printing an extra line to clean up the console
    user_command = input("Command: ")
    
    if user_command == "add":
        item = input("Item: ")
        shopping_list.append(item)
        print(item + " was successfully added to the shopping list!")
    
    if user_command == "delete":
        item = input("Item: ")
        shopping_list.remove(item)
        print(item + " was successfully removed from the shopping list!")
    
    if user_command == "print":
        i = 1
        print("Shopping List:")
        
        for list_item in shopping_list:
            print(str(i) + ": " + list_item)
            i = i + 1

Output:


Command: add
Item: spinach
spinach was successfully added to the shopping list!

Command: add
Item: chocolate
chocolate was successfully added to the shopping list!

Command: add 
Item: dog food
dog food was successfully added to the shopping list!

Command: print
Shopping List:
1: spinach
2: chocolate
3: dog food

Command: delete
Item: spinach
spinach was successfully removed from the shopping list!

Command: print
Shopping List:
1: chocolate
2: dog food    

The str() is used to change data from an integer or float type to a string data type. This basically converts the data to text that we can use in strings, print statements, etc.

If you don’t understand something, feel free to ask us on our Discord server or simply contact us.

chevron_up