Reputation: 135
I'm trying to get a multiplication inside a for loop working on the code below:
#products list and it's values per unity
tomato = 2
potato = 3
carrot = 1
pricePaid = int()
print("Welcome to my grocery shop!\n")
productList = ["Potato", "Tomato", "Carrot"]
print("What will you want today?\n""We have:")
print(*productList, sep=', ')
product = input("What will it be? ")
quantity = int(input("And how many do you want? "))
for productChoice in product:
if productChoice == "Potato":
pricePaid = quantity * potato
elif productChoice == "Tomato":
pricePaid = quantity * tomato
elif productChoice == "Carrot":
pricePaid = quantity * carrot
print("Here's your bag with {0} {1}s. The total is ${2:.2f}.".format(quantity, product, pricePaid))
but I'm getting a $0.00 as my results where it should be the quantity * productPrice:
"Here's your bag with 1 Tomatos. The total is $0.00."
Why is it failing? What am I missing here?
I'm using Python 3.x
Thanks in advance!
Upvotes: 3
Views: 451
Reputation: 18906
Here an improved code using dictionaries. Dictionaries alone are worth studying to make code much lighter. Basically a dictionary is composed by keys (for instance tomato) holding values (for instance 1). You can get those values by using .get().
#products list and it's values per unity
prices = dict(tomato=2,potato=3,carrot=1)
product = input('''\
Welcome to my grocery shop!
What will you want today?
We have: {}
What will it be?
'''.format(', '.join(prices.keys()))).lower()
quantity = int(input("And how many do you want? "))
pricePaid = prices.get(product,0) * quantity
if pricePaid:
print("Here's your bag with {0} {1}s. The total is ${2:.2f}."
.format(quantity, product, pricePaid))
else:
print("Error with input!")
Upvotes: 1
Reputation: 101
This is causing your issues:
for productChoice in product:
if productChoice == "Potato":
pricePaid = quantity * potato
elif productChoice == "Tomato":
pricePaid = quantity * tomato
elif productChoice == "Carrot":
pricePaid = quantity * carrot
If we changed that to this quickly we can see why
for productChoice in product:
print(productChoice)
The output to that with product being "Tomato"
T
o
m
a
t
o
What you are doing here is iterating over each character in the string product
, when you don't actually want this behavior. The fix to your problem is just removing the for loop, keeping the selection only.
This is all you need:
if product == "Potato":
pricePaid = quantity * potato
elif product == "Tomato":
pricePaid = quantity * tomato
elif product == "Carrot":
pricePaid = quantity * carrot
Hope this helps!
Upvotes: 2
Reputation: 78650
You are iterating over the letters in producChoice
.
Add a print(productChoice)
to your loop in order to watch what happens.
Since no single letter equals one of your products, none of the conditional statements will be triggered and pricePaid
will stay at its original value of int() == 0
.
There's no need for the for
loop here at all, so just remove it.
Upvotes: 1
Reputation: 7130
You're iterating through the characters in the input, and setting the price paid each time. There's no need to iterate, as product
doesn't change. Removing that for
loop does the trick. You also need to remove the references to productChoice
, since it (literally) has no purpose.
#products list and it's values per unity
tomato = 2
potato = 3
carrot = 1
pricePaid = int()
print("Welcome to my grocery shop!\n")
productList = ["Potato", "Tomato", "Carrot"]
print("What will you want today?\n""We have:")
print(*productList, sep=', ')
product = input("What will it be? ")
quantity = int(input("And how many do you want? "))
if product == "Potato":
pricePaid = quantity * potato
elif product == "Tomato":
pricePaid = quantity * tomato
elif product == "Carrot":
pricePaid = quantity * carrot
print("Here's your bag with {0} {1}s. The total is ${2:.2f}.".format(quantity, product, pricePaid))
Upvotes: 2