Python Forum
Vending Machine Program: Problem with variables
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vending Machine Program: Problem with variables
#1
I'm trying to make a simple vending machine program. Running this program gives me this error:

Error:
Traceback (most recent call last):  File "C:/Users/ignacio cabero/Desktop/Python/vendingmachine.py", line 13, in <module>    insertp25()  File "C:/Users/ignacio cabero/Desktop/Python/vendingmachine.py", line 11, in insertp25    total_insert += 0.25 UnboundLocalError: local variable 'total_insert' referenced before assignment
global total_cost, total_insert
total_cost=0
total_insert=0

float(total_cost)
float(total_insert)
def insertp25():
   total_insert += total_insert + 0.25
   print(total_insert)
insertp25()
insertp25()
What can I do? I made the variables global yet nothing seems to work.
Reply
#2
global statement is for use in a function to reference to a global variable not local
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
to elaborate further on what wavic said, here is how it should be

total_insert=0.0

def insertp25():
    global total_insert
    total_insert +=  0.25
    print(total_insert)
insertp25()
insertp25()
note that your line 8 is incorrect. it should be either like in my code above (line 5) or like this

total_insert = total_insert + 0.25
note that using globals is considered bad programming practice. you should pass arguments to functions instead.

total_insert=0.0

def insertp25(total):
    total +=  0.25
    print(total)
    return total

total_insert = insertp25(total_insert)
total_insert = insertp25(total_insert)
That said, it's obvious that using function is somewhat redundant. It only complicates the code. In a more advanced approach you would use class, e.g.
class VendingMachine(object):
    accepted_coins = (0.25, 0.50, 1.00, 2.00)
    drinks = {'coffee':0.50, 'tee':1.00}

    def __init__(self):
        self.total = 0.00

    def insert_coin(self, coin):
        if float(coin) not in self.accepted_coins:
            print('The machine accepts only: {}.'.format(self.accepted_coins), end=' ')
        else:
            self.total += coin
        print('Total of {:.2f} in the machine'.format(self.total))

    def buy(self, buy_drink):
        buy_drink = buy_drink.lower()
        if buy_drink not in self.drinks:
            print("We don't have {}. You can buy only:".format(buy_drink))
            for drink, price in sorted(self.drinks.items()):
                print('{} for {:.2f}'.format(drink, price))
        elif self.total < self.drinks[buy_drink]:
            print('Not enough money in the machine. Please insert {:.2f} more'.format(self.drinks[buy_drink] - self.total))
        else:
            self.total -= self.drinks[buy_drink]
            print('Take your {}. Total of {:.2f} in the machine'.format(buy_drink, self.total))



vm = VendingMachine()
vm.insert_coin(0.25)
vm.insert_coin(0.25)
vm.insert_coin(0.75)
vm.insert_coin(1.0)
vm.buy('tee')
vm.buy('tee')
vm.insert_coin(0.50)
vm.buy('tee')
vm.buy('juice')
Output:
Total of 0.25 in the machine Total of 0.50 in the machine The machine accepts only: (0.25, 0.5, 1.0, 2.0). Total of 0.50 in the machine Total of 1.50 in the machine Take your tee. Total of 0.50 in the machine Not enough money in the machine. Please insert 0.50 more Total of 1.00 in the machine Take your tee. Total of 0.00 in the machine We don't have juice. You can buy only: coffee for 0.50 tee for 1.00
Of course, this is only sample implementation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem using exec to save local variables dkarl 0 1,801 Dec-01-2019, 08:52 AM
Last Post: dkarl
  Problem with variables JCB 3 2,309 Nov-13-2019, 07:20 PM
Last Post: the_ophidian_order
  Problem: Once I cancel the process my program will start working! Hadad 0 1,653 Jul-24-2019, 04:09 PM
Last Post: Hadad
  Program Problem (2) ChrisG 2 2,397 May-25-2019, 05:11 PM
Last Post: ChrisG
  Program Problem ChrisG 2 1,972 May-25-2019, 11:41 AM
Last Post: ChrisG
  A problem with defining variables meru120 5 3,410 Apr-16-2019, 03:13 PM
Last Post: buran
  Problem with variables Steffenwolt 4 3,292 Jul-14-2018, 07:27 PM
Last Post: Steffenwolt
  problem running program in VScode LavaCreeperKing 4 3,895 Mar-25-2018, 04:34 PM
Last Post: LavaCreeperKing
  Program not running (Overloading problem) anurag123 2 2,567 Feb-19-2018, 07:23 PM
Last Post: nilamo
  [split] Problem using global variables RedSkeleton007 1 2,659 Nov-10-2017, 09:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020