Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using python 2.7.13. I had to make a program that inputs a product type and then totals the amount for all the products entered. I tried making one but it isn't working.
there are total 5 types of products that can be entered. Here are their names and prices
bun 0.50
coffee 1.20
cake 1.50
sandwich 2.10
dessert 4.00

What I have tried:

Python
while True:
    item = str(input("Which item would you like to buy?"))
    elif item == 'bun':
        abun = abun + 0.50
        nbun += 1
        continue
    elif item == 'coffee':
        acoffee = acoffee + 1.20
        ncoffee += 1
        continue
    elif item == 'cake':
        acake = acake + 1.50
        ncake += 1
        continue
    elif item == 'sandwich':
        asandwich = asandwich + 2.10
        nsandwich += 1
        continue
    elif item == 'dessert':
        adessert = adessert + 4.00
        ndessert += 1
        continue
    user_input = raw_input('type yes to continue and end to terminate:')
    if user_input == 'end':
        break
    total = abun + acoffee + acake + asandwich + adessert
Posted
Updated 1-Aug-17 23:44pm
v3

Hi try this
I assume you are initailising all variables to zero

abun = 0
acoffee=0
acake=0
asandwich=0
adessert=0
nbun=0
ncake=0
ncoffee=0
nsandwich=0
ndessert=0



while True:
    item = raw_input("Which item would you like to buy?")
    if item == 'bun':
        abun = abun + 0.50
        nbun += 1
    elif item == 'coffee':
        acoffee = acoffee + 1.20
        ncoffee += 1
    elif item == 'cake':
        acake = acake + 1.50
        ncake += 1
    elif item == 'sandwich':
        asandwich = asandwich + 2.10
        nsandwich += 1
    elif item == 'dessert':
        adessert = adessert + 4.00
        ndessert += 1
    user_input = raw_input('type yes to continue and end to terminate:')
    if user_input == 'end':
        break
total = abun + acoffee + acake + asandwich + adessert
 
Share this answer
 
v2
Comments
Member 13340390 2-Aug-17 4:16am    
This works thanks but it still got a problem in there. If I enter three types of items for example three of them are 'bun' the total output should be 1.50 whereas this program gives output 1.00 why is that so
fixed the solution above total has to be outside the while loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900