When I wrote this program, I have been learning Python for nearly one hundred hours, I saw someone on CSDN asking for help using Python how to write a program to sell drinks automatically, I thought, try to write a practical vending program. Of course, just to realize the basic functions, welcome to expert guidance, novice learning reference.
Running environment: Python 2.7
# encoding=UTF-8 loop=True money=0 while loop: x = raw_input('Hint: Please put in gold coins, and press "q" to end the coin toss.') if x=='q': if money==0: print 'Error: you have not put in a bill, please put in at least one bill before shopping for products' else: print ''Hint: You have ended the coin toss and will enter the purchase goods operation interface'' loop = False else: try: x = int(x) money+=x print 'Hint: You're coin tossing this time',x,'RMB, your total coins.',money,'Yuan Renminbi' except Exception,e: print 'Error: your gold coins are not recognized by the system, please re-coin, thank you!' GoodList = { 'Coca-Cola':2.5, 'Fruit Orange':3, 'Milk tea':1.5, 'Gadobao':4 } i=0 print 'Please select a commodity:' for x in GoodList: i+=1 print 'Number',i,'Trade name',x,'Price',GoodList[x] print fanwei = range(len(GoodList)) loop = True while loop: o = raw_input('Hint: Please enter the item number you want to buy and press "q" to end the purchase') 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 'Hint: The item you purchased is:',x,', Price:',GoodList[x],', you also have remaining: ',money,'Yuan Renminbi' if money==0: loop = False else: print 'Error: your balance',money,'There are not enough dollars left to buy this item',x,'[',GoodList[x],'Meta]' else: print 'Error: The item number you have entered does not exist, please re-enter it' except Exception,e: print 'Error: please enter the correct product number, thank you for your cooperation!' if money>0: print 'Hint: the system will be looking for you,',money,'Yuan RMB, welcome to the next visit' else: print 'Hint: Your balance has been used up, welcome to the next visit'