下面小编就为大家带来一篇Python初学时购物车程序练习实例(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
废话不多说,直接上代码
#Author:Lancy Wu product_list=[ ('Iphone',5800), ('Mac Pro',9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ('Lancy Python', 120) ] #商品列表 shopping_list=[] #定义一个列表来存储已购商品 salary=input("请输入工资:") if salary.isdigit(): #当输入的内容为数字 salary=int(salary) #将输入的工资转换成int类型 while True: #循环打印出所有商品列表,有两种写法,一般用下面一种 # for item in product_list: # print(product_list.index(item),item) for index,item in enumerate(product_list): #enumerate()这个方法是取出列表下标 print(index,item) user_choice=input("是否购买商品?如果要购买商品请输入商品编号:") if user_choice.isdigit(): #当输入的商品编号为数字 user_choice=int(user_choice) #将输入的商品编号转换成int类型 if user_choice<len(product_list) and user_choice>=0: #判断输入的商品编号是否存在 p_item=product_list[user_choice] #根据商品下标取出所购买的商品 if p_item[1]<=salary: #当商品的价格小于等于余额 shopping_list.append(p_item) #将购买的商品存储到shopping_list[]列表中 salary-=p_item[1] #计算余额 print("您购买的商品为%s,余额为\033[31;1m%s\033[0m"%(p_item,salary)) #这个地方做了一下处理,将余额高亮显示 else: #当商品的价格大于余额 print("\033[41;1m你的余额只剩[%s]\033[0m"%salary) #这个地方做了一下处理,将余额高亮显示 else: print("该商品不存在!") elif user_choice=='q': #当输入的商品编号为q时,打印购买的商品和余额并退出程序 print("--------以下是购买的商品--------") for p in shopping_list: print(p) print("您的余额为:",salary) exit() else: print("该商品不存在!")
以上是Python實作購物車的簡單實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中