温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python购物车程序简单代码

发布时间:2020-08-24 00:46:02 来源:脚本之家 阅读:142 作者:不会代码的程序员 栏目:开发技术

本文实例为大家分享了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购物车程序简单代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI