0

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?")
SteveS
  • 3
  • 1
  • 3
  • `inp != value` compares the values of `inp` and `value`. You'll want to check if `value` [contains](http://stackoverflow.com/questions/12934190/is-there-a-short-contains-function-for-lists-in-python) `inp`. – dirn Apr 15 '14 at 18:34
  • `input` will return a string, not an integer, so be careful about how you compare types. – dirn Apr 15 '14 at 18:35
  • @dirn so how would I do that? Would it have to be input(int( ? – SteveS Apr 15 '14 at 18:42
  • You can either use `inp = int(input(...))` (this could raise a `ValueError` if the user's input can't be converted to an integer, and it only works for one value) or you can use strings in `values = ['10', '20', ...]`. – dirn Apr 15 '14 at 19:17

1 Answers1

2

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.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • thanks bro, it worked:)) The only thing I don't understand is how coin is not an invalid syntax? – SteveS Apr 15 '14 at 18:39
  • @SteveS while we are going over each value in `inp`, coin just represents the current value that we are analyzing, it doesn't exist as a variable globally, it is just a representation. That is why there is no error. – anon582847382 Apr 15 '14 at 18:45
  • 1
    This is a very pretty bit of code using a *generator expression*. `for coin in inp.split()` says "take the list created by `inp.split()` and assign each value in that list in turn to `coin`. For each value of `coin`, we evaluate the expression `int(coin) not in value` (meaning "`true` if the value of coin is *not* in the array `value`, `false` if not); that resulting list of booleans gets passed to `any()`, whose value is `true` if any value in the list was `true`, `false` otherwise. A spiffy piece of code in a single expression. – Joe McMahon Apr 15 '14 at 18:50
  • 1
    @JoeMcMahon Very correct and thank you for giving a much better explanation than me. However it is not a list comprehension but a generator expression. – anon582847382 Apr 15 '14 at 18:51
  • You can tell I'm not primarily a Python programmer! - thanks for the correction. Fixed. – Joe McMahon Apr 15 '14 at 18:52
  • @JoeMcMahon Thanks for the help both of you, appreciate it!! – SteveS Apr 15 '14 at 19:07