本文摘自php中文网,作者黄舟,侵删。
下面小编就为大家带来一篇Python初学时购物车程序练习实例(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧废话不多说,直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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)
while True :
for index,item in enumerate (product_list):
print (index,item)
user_choice = input ( "是否购买商品?如果要购买商品请输入商品编号:" )
if user_choice.isdigit():
user_choice = int (user_choice)
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)
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' :
print ( "--------以下是购买的商品--------" )
for p in shopping_list:
print (p)
print ( "您的余额为:" ,salary)
exit()
else :
print ( "该商品不存在!" )
|
以上就是Python实现购物车的简单实例分享的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python如何求水仙花数?
Python换行符怎么用?
Python是什么编程语言
Python中构造方法的解析(附示例)
Python中numpy是什么
Python使用re模块正则提取字符串中括号内的内容
Python是什么情况下诞生的
介绍Python应用学习之qrcode生成二维码
Python怎么做反爬
Python使用迭代器捕获generator返回值的方法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现购物车的简单实例分享