How do you allow only inputs from a list? (python vending machine) -


i need code vending machine, accepts coins

"it allow input 1p, 2p, 5p, 10p, 20p, 50p , £1.00 reject £2.00 coin"

i have list, float values inside:

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

these coins, wanting user enter

coin = float(input()) 

and after have

def balance():      coin = float(input("please enter coins."))      if coin not in coins:             print("incorrect coin amount! please remember don't accept 2 pound coins!")       else:         print("correct coin amount. coins have been added balance")         fb = fb + coin 

i couldn't work, print "incorrect coin amount! please remember don't accept 2 pound coins!". after this, tried solution on here: python coding - vending machine - how user enter coins? thought meant needed change float(input()) , float int, changing 0.01 (1p) 1. but, when did, stil got

'int' object has no attribute 'split' 

when using in 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 coins. please enter in pence, example 1 pound = 100"))     if any(int(coin) not in value coin in inp.split()):         print("machine doesn't accept these coins")   else:     print("correct coin amount. coins have been added 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 while loop, ignore fb = 0.00 balance() print("your full balance is",fb) 

i'd recommend converting pounds pence don't have float math, , convert time you're displaying values. use decimal module this, let's not involved in yet. ultimate problem seems you're comparing different types of values, , 1 != "1". let's sort 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 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -