Python shopping cart program ๐Ÿ›’

Share it with your friends Like

Thanks! Share it with your friends!

Close

#python #tutorial #course

# Shopping cart exercise

foods = []
prices = []
total = 0

while True:
food = input(“Enter a food to buy (q to quit): “)
if food.lower() == “q”:
break
else:
price = float(input(f”Enter the price of a {food}: $”))
foods.append(food)
prices.append(price)

print(“—– YOUR CART —–“)

for food in foods:
print(food, end=” “)

for price in prices:
total += price

print()
print(f”Your total is: ${total}”)

Comments

Bro Code says:

# Shopping cart exercise

foods = []

prices = []

total = 0

while True:

food = input("Enter a food to buy (q to quit): ")

if food.lower() == "q":

break

else:

price = float(input(f"Enter the price of a {food}: $"))

foods.append(food)

prices.append(price)

print("—– YOUR CART —–")

for food in foods:

print(food, end=" ")

for price in prices:

total += price

print()

print(f"Your total is: ${total}")

Rayton Satkek says:

class Product:
def __init__(self, product_id, product_name, unit_price):
self.product_id = product_id
self.product_name = product_name
self.unit_price = unit_price

def display_product_details(self):
print("Product ID:", self.product_id)
print("Product Name:", self.product_name)
print("Unit Price:", self.unit_price)

# Example usage:
product1 = Product(1, "Widget", 10.99)
product1.display_product_details()

Tried running on PC and not working, please help

22AT1A04C9 SK NATIQ AHMED says:

digital clock
import time

p=input("specify whether ypo are entering time in hours or minutes or seconds:")

t=int(input("Enter time:"))

if p == "hours":

t=t*60*60

elif p=="minutes":

t*=60

else:

t=t

for x in range(t,0,-1):

sec=x%60

min=int(x/60)%60

hour=int(x/3600)%60

print(f"{hour:02}:{min:02}:{sec:02}")

time.sleep(1)

print("Time is up!")

22AT1A04C9 SK NATIQ AHMED says:

credit card validator
sum_of_odd_digits=0

sum_of_even_digits=0

total=0

cc=input("Enter your credit card number:")

cc=cc.replace("-","")

cc=cc.replace(" ","")

cc=cc[::-1]

for x in cc[::2]:

sum_of_odd_digits+=int(x)

for x in cc[1::2]:

x=int(x)*2

if x>=10:

sum_of_even_digits+=(1+(x%10))

else:

sum_of_even_digits+=x

total=sum_of_even_digits+sum_of_odd_digits

if (total%10 ==0):

print("VALID")

else:

print("INVALID")

22AT1A04C9 SK NATIQ AHMED says:

shopping cart program
foods=[]

prices=[]

total=0

while total>=0:

food=input("Enter the item (q to quit): ")

if (food=="Q")or(food=="q"):

break

else:

price=float(input(f"The price of the item is $"))

foods.append(food)

prices.append(price)

print("******Your items are******")

for food in foods:

print(food)

for price in prices:

total+=price

print(f"your total is {total}")

inday sa balitaw says:

youre so smart, i cant figure out the foods.append(food) i cant fix it, it means my knowledge in python is still off

Adi Livni says:

You should give flow to the price +=

Adi Livni says:

You forgot to put flow in the price.

Pet Me says:

6:43 lol you type so fast XD

A dream footage says:

Don't stop bro!!! Greate videos

space7n stuff says:

What's the purpose of the f in the beginning on the string?

Beeposky says:

how do you declare a list? ive atempted to make something similar to this but my lists are getting the undeclared error

aFirma says:

Ok, so I tried making this output look better, but failed at displaying the cart with proper alignment, can anyone help?
I think this line of the code needs to be modified, print(f"{i}. {food.capitalize()} x {quantity:<13}", end=""), but I maybe (I am) wrong.

# Shopping Cart

foods = []

prices = []

quantities = []

total = 0

print("Enter your food products. Press Enter to quit. ")

i = 1

while True:

food = input(f"{i}. ")

if food == "":

break

else:

price = float(input(f"Price of {food}: "))

quantity = int(input(f"Number of {food}: "))

foods.append(food)

prices.append(price)

quantities.append(quantity)

i += 1

print("——— YOUR CART ———")

i = 1

if len(foods) == 0:

text = "Your cart is empty."

print(f"{text:^19}")

else:

for food in foods:

price = prices[i – 1]

quantity = quantities[i – 1]

print(f"{i}. {food.capitalize()} x {quantity:<13}", end="")

print(f"${price * quantity}")

total += price * quantity

i += 1

print("—————————–")

if not len(foods) == 0:

print(f"Your total is: ${total}")

Keikoo says:

And pay in Bitcoin ?

Doga Ozer says:

@Bro Code which IDE are you using? ๐Ÿ™‚

ะฐะฝะธั‡ั‘ says:

foods = []

prices = []

while True:

food = input("Enter a food to buy (q to quit): ")

if food.lower() == "q":

break

else:

price = float(input(f"Enter the price of a {food}: $"))

foods.append(food)

prices.append(price)

print("Your cart: ")

i = 0

while i < len(foods):

print(f"{foods[i]} costs ${prices[i]}")

i += 1

print(f"total is: {sum(prices)}")

Nadya Shaymardanova says:

ะžั‡ะตะฝัŒ ะธะฝั‚ะตั€ะตัะฝะพ

Nalani Hamby says:

Iโ€™m so confused, Iโ€™ve got everything typed exactly as it is on the video but when I get to the price it throws an error and tells me โ€˜strโ€™ object has no attribute โ€˜appendโ€™ why clue what I am doing wrong?

ja kub says:

i love your videos

James Mwai says:

Surprisingly, I am getting an " unsupported operand type(s) for +: int and list" error. Here is my code
foods = []

prices = []

total = 0

while True:

food = input(" Enter a food to buy('q' to quit): ")

if food.lower() == "q":

break

else:

price = float(input(f"Enter the price of a {food}: Kshs "))

foods.append(food)

prices.append(prices)

print ("—–Your Cart —–")

for food in foods:

print (food, end= ' ')

for price in prices:

total += price

print(f"Your total is Kshs{total}")

What could be wrong here?

Write a comment

*