Python: vending machine | Vending machine Python

Python: vending machine ... The program offers items, has the user pay for items, 'gives' the items, and updates the stock of each item. It seems ...HomePublicQuestionsTagsUsersCollectivesExploreCollectivesFindaJobJobsCompaniesTeamsStackOverflowforTeams–Collaborateandshareknowledgewithaprivategroup.CreateafreeTeamWhatisTeams?TeamsCreatefreeTeamCollectivesonStackOverflowFindcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.LearnmoreTeamsQ&AforworkConnectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.LearnmorePython:vendingmachineAskQuestionAsked2years,11monthsagoActive2years,11monthsagoViewed14ktimes0Theprogramoffersitems,hastheuserpayforitems,'gives'theitems,andupdatesthestockofeachitem.Itseemstoworkmoreorless.Ihave2questions:1)Idon'tknowhowtoexitthethevendingmachineprogramaftertheuseranswers'n'to'buysomethingelse?(y/n):'.2)Isthereawaytomakethecodesimpler/better?defvend():a={'item':'choc','price':1.5,'stock':2}b={'item':'pop','price':1.75,'stock':1}c={'item':'chips','price':2.0,'stock':3}d={'item':'gum','price':0.50,'stock':1}e={'item':'mints','price':0.75,'stock':3}items=[a,b,c,d,e]cim=0#cashinmachineprint('welcometovendingmachine!\n***************')#showitems,pricesdefshow(items):print('\nitemsavailable\n***************')foriteminitems:ifitem.get('stock')==0:items.remove(item)foriteminitems:print(item.get('item'),item.get('price'))print('***************\n')#haveuserchooseitemwhileTrue:show(items)selected=input('selectitem:')foriteminitems:ifselected==item.get('item'):selected=itemprice=selected.get('price')whilecim

常見投資理財問答


延伸文章資訊