SteveS
SteveS

Reputation: 3

Python Coding - Vending machine - How to get the user to only enter certain coins?

I'm doing a controlled assessment on python. One of the tasks is to create a vending machine under certain criteria. I'm pretty bad a python and I'm probably being an idiot and doing this wrong.

I want the user to input only 10,20,50,1.00 coins. If the user enters anything other than these coins, I want it to print "Machine doesn't accept these coins".

This is what I have so far:

inp = input("Enter Coins, Note: Machine only accepts 10, 20, 50 and 100 coins: ")
value = [10,20,50,100]
if inp != value:
    print("Machine doesn't accept these coins")
else:
    print("What would you like to buy?")

Upvotes: 0

Views: 3332

Answers (1)

anon582847382
anon582847382

Reputation: 20391

Here, you want:

if any(int(coin) not in value for coin in inp.split()):
    print("Machine doesn't accept these coins")

What this basically does it split up the input into separate coins, converts them to integers, (because the items in values are integers) then checks if it is not in values, which of course would mean it is invalid.

Finally, this is done until it finds an invalid coin (take a look at any). At that, it will print that the coins are invalid. If it does not, then it will continue to else.

Upvotes: 2

Related Questions