python购物车程序简单代码

yipeiwu_com6年前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模拟登录百度代码分享(获取百度贴吧等级)

复制代码 代码如下:# -*- coding: utf8 -*-'''Created on 2013-12-19 @author: good-temper''' import urlli...

django从请求到响应的过程深入讲解

django从请求到响应的过程深入讲解

django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用djan...

Python使用pygame模块编写俄罗斯方块游戏的代码实例

Python使用pygame模块编写俄罗斯方块游戏的代码实例

文章先介绍了关于俄罗斯方块游戏的几个术语。 边框——由10*20个空格组成,方块就落在这里面。 盒子——组成方块的其中小方块,是组成方块的基本单元。 方块——从边框顶掉下的...

python网络编程实例简析

本文实例讲述了python网络编程,分享给大家供大家参考。 具体方法如下: 服务端代码如下: from SocketServer import(TCPServer as TCP,...

PyQt Qt Designer工具的布局管理详解

PyQt Qt Designer工具的布局管理详解

前言 这节课很重要。。界面整洁美观与否就看布局了。。这里讲布局方法,至于设计的天赋与最终界面的美感那就看造化了。。 本文主要讲述Qt Designer工具实现界面控件布局管理,就是排列组...