Skip to content
Sign upLog in
← Back to Community

Add to cart.

Profile icon
Ardin16

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

Voters
Profile icon
VisheshSanghvi
Profile icon
Ardin16
Comments
hotnewtop
Profile icon
Roar123

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!

print(cart)

Tell me if you were able to follow

Profile icon
BlueComet

I forked your repl and tweeked it I think this is what you want if its not then tell me heres the link https://repl.it/@iterate/Projects

Profile icon
TheDrone7

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