This article mainly introduces the code sharing of an Automatic Beverage Sales program implemented by Python, that is, the internal program judgment code of a coin-operated automatic beverage sales machine implemented by python, I have been learning Python for nearly one hundred hours. On CSDN, I saw someone asking for help using Python to write a program for auto-selling drinks, I want to try to write a practical vending program. Of course, it is just to implement basic functions. You are welcome to give pointers and learn from new users.
Running Environment: Python 2.7
# Encoding = UTF-8loop = Truemoney = 0 while loop: x = raw_input ('prompt: Please input gold coins, end coin press "q" Key') if x = 'q ': if money = 0: print 'error: You have not invested a banknote. Please invest at least one banknote before you can purchase the 'else: print'. Tip: You have ended coin writing, on the product purchase page, click 'loop = False else: try: x = int (x) money + = x print '. The system prompts you to pay for this coin', x, 'RMB, you have invested a total of coins ', money,' Renminbi 'except t Exception, e: print' error: Your gold coin system is not recognized. Please try again. Thank you! 'Goodlist = {'Coca-Cola ': 2.5, 'Fruit Orange': 3, 'milk tea': 1.5, 'canadian treasure ': 4} I = 0 print' select a product: 'for x in GoodList: I + = 1 print' No. ', I, 'item name', x, 'price ', goodList [x] print fanwei = range (len (GoodList) loop = Truewhile loop: o = raw_input ('prompt: Enter the product number you want to purchase, press the "q" key to end purchasing ') if o = 'q': loop = False else: try: o = int (o) if o> = 1 and o <= len (GoodList): I = 0 for x in GoodList: I + = 1 if I = o: if money> = GoodList [x]: money-= GoodList [x] print 'prompt: The items you purchased are: ', x,', price: ', GoodList [x],', and you still have: ', money, 'yuan Renminbi 'if money = 0: loop = False else: print' error: Your balance ', money,' the dollar is insufficient to buy this item ', x, '[', GoodList [x], 'yuan]' else: print 'error: the product number you entered does not exist. Please enter 'failed t Exception, e: print' again. Error: enter the correct product number. Thank you for your cooperation! 'If money> 0: print 'prompt: the system will look for you,', money, 'RMB. Welcome to 'else: print' next time. Prompt: Your balance has been used up, welcome'