0

I need to code a vending machine, that only accepts certain coins

"It will allow you to input 1p, 2p, 5p, 10p, 20p, 50p and £1.00 but it will REJECT a £2.00 coin"

I have a list, with float values inside:

coins = ["0.01","0.02","0.05","0.10","0.20","0.50","1"]

These are the coins, that I am wanting the user to enter into

coin = float(input())

And after this I have

def balance():
     coin = float(input("Please enter your coins."))
     if coin not in coins:
            print("Incorrect coin amount! Please remember we don't accept 2 pound coins!")

     else:
        print("Correct coin amount. The coins have been added to your balance")
        fb = fb + coin

I couldn't get this to work, as it would just print "Incorrect coin amount! Please remember we don't accept 2 pound coins!". After this, I tried this solution already on here: Python Coding - Vending machine - How to get the user to only enter certain coins? I thought this meant I needed to change my float(input()) and everything float to int, so changing 0.01 (1p) to 1. But, when I did, I stil got

'int' object has no attribute 'split'

When using it in this code

dict = {"KitKat":"80p", "Coca-Cola":"85p", "DairyMilk":"80p","Walkers Crisps":"90p"}
coins = ["1","2","5","10","20","50","100"]

def balance():
    inp = int(input("Please enter your coins. Please enter in pence, for example 1 pound = 100"))
    if any(int(coin) not in value for coin in inp.split()):
        print("Machine doesn't accept these coins")


else:
    print("Correct coin amount. The coins have been added to your balance")
    fb = fb + coin


def items():
    print (" 1. KitKat:" , dict['KitKat'])
    print (" 2. Coca-Cola:", dict['Coca-Cola'])
    print (" 3. Dairy Milk:", dict["DairyMilk"])
    print (" 4. Walkers Crisps:", dict["Walkers Crisps"])
snack = 1 # need a while loop, ignore
fb = 0.00
balance()
print("Your full balance is",fb)
Community
  • 1
  • 1

1 Answers1

1

I'd recommend converting pounds to pence so you don't have to do float math, and just convert back any time you're displaying values. You could also use the decimal module for this, but let's not get too involved in that yet. Your ultimate problem seems to be that you're comparing different types of values, and 1 != "1". Let's sort that first.

coins = [1, 2, 5, 10, 20, 50, 100]  # pence
coin_in = int(input("Enter amount (in pence): "))

if coin_in not in coins:
    # incorrect input, handle it
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • might as well use a set of coins – Padraic Cunningham Oct 02 '15 at 20:53
  • @PadraicCunningham yes, just didn't want to throw anything new at them while they're still trying to figure out what they've already been taught. A set `set([1, 2, 5, 10, 20, 50, 100])` (or `{1, 2, 5, 10, 20, 50, 100}`) would be the perfect data type for this, but I doubt that OP has learned about them yet – Adam Smith Oct 02 '15 at 20:55
  • Never too early to learn about efficiency ;) I thinks set are an important thing to learn about they are often the difference between quadratic and linear runtime. – Padraic Cunningham Oct 02 '15 at 20:57
  • @AdamSmith Correct, I haven't learnt about sets yet, nor most of the stuff in this homework. So if I change coins = [] to coins ([]), would it be a better script? Thanks for the help, worked perfectly, gotta fix a couple errors that I've somehow generated, though :) – user3670603 Oct 02 '15 at 21:12
  • 1
    @user3670603 The syntax is `set([1, 2, 5, ...])`. Or you can use the literal notation which looks like a dictionary with no values, only keys `{1, 2, 5, ...}`. In fact, thinking of sets as dictionaries without values makes a TON of sense. Both are unordered, both have very fast (O(1)) lookups, and neither support duplicates (you can't do `{1, 1, 1, 1}` like you can do `[1, 1, 1, 1]`) – Adam Smith Oct 02 '15 at 21:25