Can someone please help me make my code work so that if the part is typed in wrong then it says a error messasge

# Computer Parts


parts = []
prices = []
total = 0

print("Computer Part List")
print("Cpu=$300")
print("Gpu=$550")
print("Cooler=$235")
print("Motherboard=$360")
print("SSD=$150")
print("Fans=$100")
print()

while True:
  
  part = input("Enter a Computer part to buy (q to quit): ")
  if part.lower() == "q":
    break
  else:
    price =  float(input(f"Enter the price of a {part}: $"))
    parts.append(part)
    prices.append(price)

print("---- Your Cart ----")

for part in parts:
  print(part)

for price in prices:
  total += price


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

Could you format it please? So we can understand it better.

1 Like

Welcome to Ask!

  1. PLEASE wrap your code in triple backticks! ```
  2. Please explain what the code is supposed to do VS. what it is doing.
2 Likes

Hey @ChristopherMeav, welcome to the forums!

Can you please provide a link to the Repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

2 Likes

I Fixed it

You had “ quotation marks instead of " quotation marks and you didn’t indent stuff

parts = []
prices = []
total = 0

print("Computer Part List")
print("Cpu=$300")
print("Gpu=$550")
print("Cooler=$235")
print("Motherboard=$360")
print("SSD=$150")
print("Fans=$100")
print()

while True:

  part = input("Enter a Computer part to buy (q to quit): ")
  if part.lower() == "q":
    break
  else:
    price = float(input(f"Enter the price of a {part}: $"))
    parts.append(part)
    prices.append(price)

    print("---- Your Cart ----")

    for part in parts:
      print(part)

    for price in prices:
      total += price

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

I would expect to be like

while True:

  part = input("Enter a Computer part to buy (q to quit): ")
  if part.lower() == "q":
    break
  else:
    price = float(input(f"Enter the price of a {part}: $"))
    parts.append(part)
    prices.append(price)

print("---- Your Cart ----")

for part in parts:
   print(part)

for price in prices:
   total += price

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

All of that was most likely correct but Discourse changed Christopher’s post because Christopher didn’t format it.

1 Like

I added a list of all parts and added an elif clause to check whether the part exists or not.

parts = []
prices = []
total = 0
all_parts = ("cpu", "gpu", "cooler", "motherboard", "ssd", "fans") #list of all parts in a common case

print("Computer Part List")
print("Cpu=$300")
print("Gpu=$550")
print("Cooler=$235")
print("Motherboard=$360")
print("SSD=$150")
print("Fans=$100")
print()

while True:
  
  part = input("Enter a Computer part to buy (q to quit): ")
  if part.lower() == "q":
    break
  elif part.lower() not in all_parts: #check whether part is wrong
    print(f"{part} is not available here...")
  else:
    price =  float(input(f"Enter the price of a {part}: $"))
    parts.append(part)
    prices.append(price)

print("---- Your Cart ----")

for part in parts:
  print(part)

for price in prices:
  total += price

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