python购物车程序简单代码

yipeiwu_com5年前Python基础

本文实例为大家分享了python购物车程序的具体代码,供大家参考,具体内容如下

代码:

''''' 
Created on 2017年9月4日 
 
@author: len 
''' 
 
 
product_list = [ 
 ('Robot',200000), 
 ('MacPro',12000), 
 ('Iphone8',8888), 
 ('Hello World',1200), 
    ] 
shopping_list = [] 
user_salary=input("请输入你的工资:") 
if user_salary.isdigit(): 
 user_salary = int(user_salary) 
 while True: 
  print("商品如下:") 
  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 > -1: 
    p_item = product_list[user_choice] 
    if user_salary>=p_item[1]: 
     shopping_list.append(p_item) 
     user_salary-=p_item[1] 
     print("购买商品",p_item,"成功您的余额为",user_salary,"元!" ) 
    else: 
     print("您的余额为",user_salary,"余额不足以购买此商品,购买失败!") 
        
   else: 
    print("并无此产品!") 
  elif user_choice == "q": 
   print("--------shopping list-------") 
   for i in shopping_list: 
    print(i) 
   exit() 
  else: 
   print("invalidate!!!")

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 使用list和tuple+条件判断详解

Python 使用list和tuple+条件判断详解

list list是一种有序的集合,可以随时添加和删除其中的元素。跟java不一样的是 可以使用arr[-1] 0>-x >=- len(arr) 索引的数字为 0~ le...

Python中的MongoDB基本操作:连接、查询实例

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可护展的高性能数据存储解决方案。它的特点是高性能、易部署、易使用,存储数据非常方便。 MongoDB...

python 计算数据偏差和峰度的方法

numpy.set_printtoptions(edgeitems=5):值过多,显示前5个和后5个 偏度:衡量随机分布的不均衡性,偏度=0,数值相对均匀的分布在两侧 峰度:概率密度在均...

django 开发忘记密码通过邮箱找回功能示例

一、流程分析: 1.点击忘记密码====》forget.html页面,输入邮箱和验证码,发送验证链接网址的邮件====》发送成功,跳到send_success.html提示 2.到邮箱里...

浅谈python中copy和deepcopy中的区别

在下是个编程爱好者,最近将魔爪伸向了Python编程。。。。。遇到copy和deepcopy感到很困惑,现在针对这两个方法进行区分,一种是浅复制(copy),一种是深度复制(deepco...