下面小编就为大家带来一篇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.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。