本文摘自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 remove()方法最有效
Python装饰器定义及运用实例讲解
Python文件的三种可读可写模式的特点及区别
如何用Python编写乘法口诀表
Python安装怎么改路径
Python是解释型语言么
Python可以开发软件吗
mysql在cmd和Python下的常用操作汇总分享
Python如何清除html文件中的内容
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现购物车的简单实例分享