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验证代理ip是否可用的实现方法

在使用爬虫爬取网络数据时,如果长时间对一个网站进行抓取时可能会遇到IP被封的情况,这种情况可以使用代理更换ip来突破服务器封IP的限制。 随手在百度上搜索免费代理IP,可以得到一系列的网...

对python:threading.Thread类的使用方法详解

Python Thread类表示在单独的控制线程中运行的活动。有两种方法可以指定这种活动: 1、给构造函数传递回调对象 mthread=threading.Thread(target...

Python的SQLalchemy模块连接与操作MySQL的基础示例

一、SQLalchemy简介 SQLAlchemy是一个开源的SQL工具包,基本Python编程语言的MIT许可证而发布的对象关系映射器。SQLAlchemy提供了“一个熟知的企业级全套...

Python实现将HTML转换成doc格式文件的方法示例

本文实例讲述了Python实现将HTML转换成doc格式文件的方法。分享给大家供大家参考,具体如下: 网页上的一些文章,因为有格式的原因,它们在网页上的源码都是带有html标签的,用cs...

python使用pygame框架实现推箱子游戏

python使用pygame框架实现推箱子游戏

本代码来源于 《Python和Pygame游戏开发指南》中的 Star Pusher 游戏,供大家参考,具体内容如下 # Star Pusher (a Sokoban clone)...