Skip to content
Sign upLog in
← Back to Community

Add to cart.

Profile icon

How do you get this to add something to "cart" and then print everything added to "cart" by printing it all under "checkout"?

Profile icon
Profile icon
Profile icon

In your case, the cart represents a list because it is storing items. To initialize it, add

cart = []

Next, we have to ask the user for some items to add to the cart. Let's have them enter space separated values or whatever you want.

add_items = input("Add items to cart (separated by spaces): ")

Finally, we need to get the items by using split() on our input.

# Splits at every occurrence of " " items = add_items.split(" ")

Now, create a for loop to append these values to cart.

for item in items: cart.append(item)

Now your cart is full of items!


Tell me if you were able to follow

Profile icon

I forked your repl and tweeked it I think this is what you want if its not then tell me heres the link

Profile icon

please try not to spoon feed. Giving the code is fine, but giving away the complete code without a decent explanation just be avoided.