-3

Hi guys I have been trying to create a working python vending machine for over a month now and it doesn't look like I am making any progress. If somebody could help me that could be great :) Here is my program so far:

print ("Welcome to the Vending Machine\n")

total = 0
dr = 1
sn = 3
money=int(input("How much money do you want to insert"))
print ("Prices: Drinks: £1, Snacks: £3\n")
Drinks = {'Coke','Pepsi','Orange Juice', 'Apple Juice','Water'}
Snacks = {'Chocolate', 'Snickers','Twix','Peanuts','Crisp'}

state = 'subtotal'
while total <= money:
    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)
else:
    print("You have exceeded your inserted money good bye")

I'm trying to make the code reject invalid input and stop when the user has gone over his spending limit.

U2EF1
  • 12,907
  • 3
  • 35
  • 37
user3662088
  • 1
  • 1
  • 1
  • 4
    What have you tried doing and how specifically did it fail? Post some examples, full error messages, etc. – Daenyth May 21 '14 at 18:32
  • 1
    Your indentation is incorrect. This code won't run. – Adam Smith May 21 '14 at 18:33
  • You may find [Asking the user for input until he gives a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-he-gives-a-valid-response) to be useful. – Kevin May 21 '14 at 18:33
  • I'm kind of intrigued by this. I think I'll work up an OOP answer in the next hour or so. – Adam Smith May 21 '14 at 18:49

1 Answers1

2

There is only one problem with this when I run this. So, here is the fix with some explanation so you can learn something. It may seem like a simple mistake but we all make them at some point and eventually, indenting will be second nature.

Indents

Unlike some langues, in Python the indentation matters. A lot. Let's look at your first if statement.

    if state != 'total':
    print('')
    print('')
    print ("\nDrinks Menue:")
    print(Drinks)
    print ("Snacks Menue:")
    print(Snacks)
    choice = input("\nEnter the item of your choice: ")

Can you see the problem? After an if statment, or more precisely, after the :, we need to indent everything else that we want to be carried out by the if statement otherwise Python won't know where to stop! So by making this simple change:

if state != 'total':
    print('')
    print('')
    print ("\nDrinks Menue:")
    print(Drinks)
    print ("Snacks Menue:")
    print(Snacks)

We have no more error and your program now runs. Notice the indents? Just check that they are always right.

Harvey
  • 384
  • 5
  • 15
  • 1
    To add on to this answer, you want your indentation to be consistent throughout your entire file. If you have a tab in one place, and spaces in another, Python won't know where one indented block starts and ends. Common convention in python is to use 4 spaces (and many editors can be configured to convert tabs to spaces). – limasxgoesto0 May 21 '14 at 20:15