#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Python Vending Machine Basic - [Help]


    Hey guys, so I was assigned my first python project, and I want to get it right. The idea is to have the amount for an item in which the user selects, and it calculates the total cost of the item subtracted from the amount the user pays, and gives total cost and returned money, sort of a receipt. The user can also choose more than one item, per say, they are asked if they would like another transaction.

    Anyways, I can't get the listings right, and I'm supposed to have the following functions: def, loop, a calculation, if statement. A basic program, as it is my first program.

    Here is the code that I have so far [Help is much appreciated].


    Code:
    ##Vending Machine
    
    print ("              Welcome to the Python Killers Vending Machine\n")
    
    total = 0
    dr = 5
    sn = 10
    
    print ("Prices: Drinks: $5, Snacks: $10\n")
    print ("If you want to be surprised, and have a random choice, type: 'random'\n")
    
    Drinks = {'Coke','Pepsi','Water'}
    Snacks = {'Hershey', 'Snikers','Twix','Peanuts','Lays'}
    
    
    
    print (Drinks)
    print (Snacks)
    
    
    
    
    FirstChoice = input ("Enter the item of your choice: ")  ##First Choices
    if FirstChoice == 'Coke' or 'Pepsi' or 'Water':
        print ("That will be a total of $",dr)
        
    elif FirstChoice == 'Hershey' or 'Snikers' or 'Twix' or 'Peanuts' or 'Lays':
        print ("That ^will be a total of $",sn)
    else:
        print ("****er")
    
    
    SecondChoice = input ("Would you like anything else? Yes/No ")
    if SecondChoice == "Yes" or "yes":
        print (Drinks)
        print (Snacks)
        if SecondChoice == (Drinks):
            print ("That will be a total of",dr)
        elif SecondChoice == (Snacks):
            print ("That will be a total of",sn)
    
    if SecondChoice == "no" or "No":
        print ("Awesome! Have a good day!")
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481

    partial advice


    # This statement is correct only for python puzzles:
    if FirstChoice == 'Coke' or 'Pepsi' or 'Water':

    if (a==1) or (a==8): # right

    A remarkably common problem. drinks is already a set, so write


    if FirstChoice in drinks:
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Maybe you'll extract ideas about loops and function definitions from this horror. We abhor repetitive code. Note that the total accumulates.
    Code:
    ##Vending Machine
    
    print ("              Welcome to the Python Killers Vending Machine\n")
    
    total = 0
    dr = 5
    sn = 10
    
    print ("Prices: Drinks: $5, Snacks: $10\n")
    print ("If you want to be surprised, and have a random choice, type: 'random'\n")
    
    Drinks = {'Coke','Pepsi','Water'}
    Snacks = {'Hershey', 'Snikers','Twix','Peanuts','Lays'}
    
    state = 'subtotal'
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print(Drinks)
        print(Snacks)
        choice = input("\nEnter the item CHANGE ME of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
            print ("****er")
        print("That ^will be a",state,"of $",total)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0
    Hey, Thanks so much for your help and reply. It is much appreciated, your reply had helped me a lot, and I see now what you did, it makes much more sense with loops.

    I really appreciate you taking the time to help me out, and write the code.


    Cheers mate.

    Here's my finished code, in case you're interested or any other viewer. I like the way you programmed it, it's clear and makes sense.

    Code:
    ##Vending Machine
    
    print ("              Welcome to the Python Killers Vending Machine\n")
    
    total = 0
    dr = 5
    sn = 10
    
    print ("Prices: Drinks: $5, Snacks: $10\n")
    Drinks = {'Coke','Pepsi','Orange Juice', 'Apple Juice','Water'}
    Snacks = {'Hershey', 'Snikers','Twix','Mama','Peanuts','Lays'}
    
    state = 'subtotal'
    
    if state != 'total':
        print('')
        print('')
        print ("\nDrinks Menue:")
        print(Drinks)
        print ("Snacks Menue:")
        print(Snacks)
        choice = input("\nEnter the item of your choice: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
        print("\n\nThat will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print ("\nDrinks Menue:")
        print(Drinks)
        print ("Snacks Menue:")
        print(Snacks)
        choice = input("\nEnter your second choice, or type 'N' to get your bill: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
        print("\n\nThat will be a",state,"of $",total)
    
    if state != 'total':
        print('')
        print('')
        print ("\nDrinks Menue:")
        print(Drinks)
        print ("Snacks Menue:")
        print(Snacks)
        choice = input("\nWould you like another choice? or type 'N' to get your bill: ")
        if choice in Drinks:
            total += dr
        elif choice in Snacks:
            total += sn
        else:
            state = 'total'
        print("\n\nThat will be a",state,"of $",total)

    Comments on this post

    • b49P23TIvg disagrees : You misunderstand. My post was a joke.

IMN logo majestic logo threadwatch logo seochat tools logo