Skip to content Skip to sidebar Skip to footer

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 doi

Solution 1:

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.

Post a Comment for "Python Coding - Vending Machine - How To Get The User To Only Enter Certain Coins?"