[Tutor] help with storing money variable

Dave Angel davea at davea.name
Tue Feb 19 13:51:28 CET 2013


On 02/19/2013 07:36 AM, Ghadir Ghasemi wrote:
> Hi guys, Iam halfway through my vending machine program that I started earlier. I ran into a problem. When the user inserts some money, The money variable is not stored for until the user buys an item. So what happens is when the users inserts some coins and then trys to buy an item the money they inserted is not stored till they buy item.Can you tell me how this can be overcome, so that when the user trys to buy an item,the vending machine correctly looks at the amount of money they have and vends the item depending on the amound of money they have.  Here is the code:
>
>
>
> def printMenu():
>      print ("|__________________________|")
>      print ("|Menu:                     |")
>      print ("|--------------------------|")
>      print ("|  1. Display all Items    |")
>      print ("|  2. Insert 10p           |")
>      print ("|  3. Insert 20p           |")
>      print ("|  4. Insert 50p           |")
>      print ("|  5. Insert £1            |")
>      print ("|  6. Buy an item          |")
>      print ("|  7. Print Current Amount |")
>      print ("|  8. Exit                 |")
>      print ("|__________________________|")
>
> while True:
>      printMenu()
>      choice = input("enter an option from the menu: ")
>
>
>
>      if choice == '1':
>          print("========================================")
>          print("         A          B            C    ")
>          print("  1.[ Vimtos ] [ Chewits ] [ Mars Bar ]")
>          print("    [  50p   ] [  40p    ] [  50p     ]")
>          print("========================================")
>          print("  2.[ Doritos ] [ Mentos ]")
>          print("    [  60p    ] [  50p   ]")
>          print("========================================")
>
>
>
>      elif choice == '2':
>          money = 0
>          number = int(input("how many 10p: "))
>          money += number * 1/10
>          print("your newly updated credit is £" + str(money) + "0")
>
>
>      elif choice == '3':
>          money = 0
>          number2  = int(input("how many 20p: "))
>          money += number2 * 2/10
>          print("your newly updated credit is £" + str(money) + "0")
>
>
>      elif choice == '4':
>          money = 0
>          number3 = int(input("how many 50p: "))
>          money += number3 * 5/10
>          print("your newly updated credit is £" + str(money) + "0")
>
>
>      elif choice == '5':
>          money = 0
>          number4 = int(input("how many £1: "))
>          money += number4 * 1
>          print("your newly updated credit is £" + str(money))
>
>
>
>      elif choice == '6':
>          code = input("Enter the code of item you want to buy e.g. for Vimtos = a1: ")
>          money = 0
>          pricea1 = 0.50
>          pricea2 = 0.60
>          priceb1 = 0.40
>          priceb2 = 0.50
>          pricec1 = 0.50
>
>      if code == 'a1':
>
>          if money > pricea1:
>              print("you have bought a packet of Vimtos and your newly updated balance is " + str(money - pricea1))
>              money -= pricea1
>          else:
>              print("you can't buy that item as your credit is too low")
>
>      if code == 'a2':
>          if money > pricea2:
>              print("you have bought a packet of Doritos and your newly updated balance is" + str(money - pricea2))
>              money -= pricea2
>          else:
>              print("you can't buy that item as your credit is too low")
>
>      if code == 'b1':
>          if money > priceb1:
>              print("you have bought a packet of Chewits and your newly updated balance is" + str(money - priceb1))
>              money -= priceb1
>          else:
>              print("you can't buy that item as your credit is too low")
>
>      if code == 'b2':
>          if money > priceb2:
>              print("you have bought a packet of Mentos and your newly updated balance is" + str(money - pricea2))
>              money -= priceb2
>          else:
>              print("you can't buy that item as your credit is too low")
>
>      if code == 'c1':
>          if money > pricec1:
>              print("you have bought a Mars Bar and your newly updated balance is" + str(money - pricea2))
>              money -= priceb2
>          else:
>              print("you can't buy that item as your credit is too low")

How did you conclude that the money they deposit isn't "stored" till 
they try to buy something?  Are you really saying it isn't visible to 
the user?  Could it be because you forgot the
         elif code == "7"

case?  (And the "8" one as well.)

The other problem I see is that many of those choices zero the value 
bound to "money".  That should be zeroed before the while loop, not 
inside any place somebody deposits something.


-- 
DaveA


More information about the Tutor mailing list