Home > Software design >  When I enter $2 and selected 1. The change amount was 0.40 i dont know whats the problem
When I enter $2 and selected 1. The change amount was 0.40 i dont know whats the problem

Time:05-30

When I enter $2 and selected 1. The change amount was 0.40 i dont know whats the problem. Below is my code.

This is a vending machine that will 1st allow user to input value then display the option and get user to insert his / her selection. And the machine will calculate how much and how many coins to give back.

while True:
    #display welcome message
    print ("Welcome to Fast Snacks vending machine")
    
    coins_inserted = float(input("Please enter the amount of coins you would like to insert: $"))
    
    if coins_inserted > 2:
        print ("Error! Please do not insert more than $2. Try again.")
        continue;
        
    elif coins_inserted == 2:
        #display items
        #abstract price
        print ("1) Calbee                     - $1.80")
        price = float(1.80)
        
        print ("2) Lays Classic Potato Chip   - $2.00")
        price = float(2)
        
        print ("3) Twisties Tomato            - $1.60")
        price = float(1.60)
        
        print ("4) KitKat Bar                 - $1.20")
        price = float(1.20)
        
        print ("5) m&m chocolate              - $1.60")
        price = float(1.60)
        
        print ("6) Skittles Sour Candy        - $1.60")
        price = float(1.60)
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        
    elif coins_inserted >= 1.80:
        #display items
        #abstract price
        print ("1) Calbee                     - $1.80")
        price = float(1.80)
        
        print ("3) Twisties Tomato            - $1.60")
        price = float(1.60)
        
        print ("4) KitKat Bar                 - $1.20")
        price = float(1.20)
        
        print ("5) m&m chocolate              - $1.60")
        price = float(1.60)
        
        print ("6) Skittles Sour Candy        - $1.60")
        price = float(1.60)
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
    
    elif coins_inserted >= 1.6:
        #display items
        #abstract price
        print ("3) Twisties Tomato            - $1.60")
        price = float(1.60)
        
        print ("4) KitKat Bar                 - $1.20")
        price = float(1.20)
        
        print ("5) m&m chocolate              - $1.60")
        price = float(1.60)
        
        print ("6) Skittles Sour Candy        - $1.60")
        price = float(1.60)
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        
    elif coins_inserted == 1.20:
        #display items
        #abstract price
        print ("4) KitKat Bar                 - $1.20")
        price = float(1.20)
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        
    else:
        print ("There aren't enough coins inserted!")
        continue;
        
    # try:
        
    change = coins_inserted - price
    
    print ("$%.2f." %change)
        
    if (selection == 1) or (selection == '01') and (change == .20):
        print ("You've selected 1 - calbee $1.80.")
        print ("1 - 20¢ left. \n")
        print ("Thank you for your purchase! Remember to take your snack and change. \n")
        
    elif (selection == 1) or (selection == '01') and (change == .10):
        print ("1 - 10¢ left. \n")
        print ("Thank you for your purchase! Remember to take your snack and change. \n")
        
    elif (selection == 1) or (selection == '01') and (change == 0):
        print ("No change. \n")
        print ("Thank you for your purchase! Remember to take your snack. \n")

CodePudding user response:

The price variable is overwritten several times before it is used, the last time with 1.6. Therefore 2-price results in 0.4.

You need a way to assign a different value to price depending on what the user entered, for example by using if statements or a look-up table implemented as a dictionary.

CodePudding user response:

As I mentioned in my comment you need to have a dictionary defined to fetch the price based on the selection from the user. The below code has items and prices dictionary then you can fetch the required information based on the selection.

prices = {'1':1.80,'2':2,'3':1.60,'4':1.20,'5':1.60,'6':1.60}
items = {'1':'Calbee','2':'Lays Classic Potato Chip','3':'Twisties Tomato',
        '4':'KitKat Bar','5':'m&m chocolate','6':'Skittles Sour Candy'}

while True:
    #display welcome message
    print ("Welcome to Fast Snacks vending machine")
    
    coins_inserted = float(input("Please enter the amount of coins you would like to insert: $"))
    
    if coins_inserted > 2:
        print ("Error! Please do not insert more than $2. Try again.")
        continue;
        
    elif coins_inserted == 2:
        #display items
        #abstract price
        print ("1) Calbee                     - $1.80")
        
        print ("2) Lays Classic Potato Chip   - $2.00")
        
        print ("3) Twisties Tomato            - $1.60")
        
        print ("4) KitKat Bar                 - $1.20")
        
        print ("5) m&m chocolate              - $1.60")
        
        print ("6) Skittles Sour Candy        - $1.60")
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        price = prices[str(selection)]
        
    elif coins_inserted >= 1.80:
        #display items
        #abstract price
        print ("1) Calbee                     - $1.80")
        
        print ("3) Twisties Tomato            - $1.60")
        
        print ("4) KitKat Bar                 - $1.20")
        
        print ("5) m&m chocolate              - $1.60")
        
        print ("6) Skittles Sour Candy        - $1.60")
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        price = prices[str(selection)]
    
    elif coins_inserted >= 1.6:
        #display items
        #abstract price
        print ("3) Twisties Tomato            - $1.60")
        
        print ("4) KitKat Bar                 - $1.20")
        
        print ("5) m&m chocolate              - $1.60")
        
        print ("6) Skittles Sour Candy        - $1.60")
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        price = prices[str(selection)]
        
    elif coins_inserted == 1.20:
        #display items
        #abstract price
        print ("4) KitKat Bar                 - $1.20")
        
        #Ask user to enter the number they want
        selection = int(input("Enter the number of the item you will want to get: "))
        print ("\n")
        price = prices[str(selection)]
        
    else:
        print ("There aren't enough coins inserted!")
        continue;
        
    # try:
        
    change = coins_inserted - price
    
    print ("$%.2f." %change)

    print ("You've selected {} - {} ${}.".format(selection, items[str(selection)], prices[str(selection)]))
    print ("1 - {}¢ left.".format(round(change,3)))
    print ("Thank you for your purchase! Remember to take your snack and change. \n")
  • Related