python练习_购物车(简版)

需求:

  1. 写一个python购物车可以输入用户初始化金额
  2. 可以打印商品,且用户输入编号,即可购买商品
  3. 购物时计算用户余额,是否可以购买物品
  4. 退出结算时打印购物小票

以下代码实现的功能与思路:

功能: 

  (1)预算金额控制,只能输入大于0的数字
  (2)商品格式化打印
  (3)选择完成要买的商品后,提示用户再次确认,确认后开始计算用户余额是否大于等于商品价格,价格正确后则加入购物车
  (4)输入q则进行结算,结算时将重复的商品进行合并,显示个数,并计算消费总额和余额

思路:

  (1)商品打印通过嵌套列表实现

  (2)购买商品是只需要比较用户余额和商品价格即可,如果比商品金额打就把这个商品加入到一个列表中

  (3)结算时计算用户购物车列表即可

流程图:

使用方法:

  执行环境:Python3.5

  执行方法,执行执行即可

代码:

 #!/usr/bin/env python
# -*- coding: utf-8 -*- import time List_items = [ ["iPhone4",100],["iPhone5",200],["iPhone6",300],["iPhone7",4000],["Python",10000],]
User_shopping_cart = [] def in_money():
'''
判断用户输入金额的函数
'''
global user_in_money
while True:
user_in_money = input("Please enter initial funds:").strip()
if user_in_money.isdigit():
if int(user_in_money) > 0:
while True:
print_lists()
else:
print("\033[31mInput Error!\033[0m")
else:
print("\033[31mInput Error!\033[0m") def print_lists():
'''
获取用户输入的编号,调用结算模块
:return:
'''
print("Product List".center(40,"-"))
Spaces = " "*2
for Product_info in List_items:
Underlined = 20-len(Product_info[0])
print(Spaces,List_items.index(Product_info)+1,Spaces,Product_info[0],"."*Underlined,Product_info[1])
print("-"*40) in_Numbering = input("Please enter the product number,[q]exit billing:").strip()
if in_Numbering.isdigit():
if int(in_Numbering) > 0 and int(in_Numbering)<= len(List_items):
Transaction_Calculations(in_Numbering)
else:
print("\033[31mThe item number does not exist!\033[0m")
else:
if in_Numbering == "q":
settlement()
else:
print("\033[31mInput Error!\033[0m") def Transaction_Calculations(Numbering):
'''
加入购物车模块,判断用户余额是否足够购买商品
'''
global user_in_money
user_in_money = int(user_in_money)
Numbering = int(Numbering)
Pu_confirmation = input("Product \033[32m%s\033[0m Whether to add to cart(y/n):"%List_items[Numbering-1][0]).strip()
if Pu_confirmation == "y":
if user_in_money >= List_items[Numbering-1][1]:
User_shopping_cart.append(List_items[Numbering-1])
user_in_money = user_in_money - List_items[Numbering-1][1]
print("Product %s Added Cart, Current Balance %s¥"%( List_items[Numbering-1][0],user_in_money))
else:
print("The balance is insufficient, the commodity price \033[31m%s\033[0m¥,lacks \033[31m%s\033[0m¥"%(List_items[Numbering-1][1],List_items[Numbering-1][1]-user_in_money))
else:
print("\033[31mNot added to cart\033[0m") def settlement():
'''
结算模块
'''
if len(User_shopping_cart) == 0:
print("Shopping Cart There are no products, thank you patronage goodbye")
exit()
else:
print("Shopping list".center(50,"-"))
consumption = 0
new_user = []
[new_user.append(i) for i in User_shopping_cart if not i in new_user]
for user_cart in new_user:
number = User_shopping_cart.count(user_cart)
settlement_un = 15-len(user_cart[0])
to_settlement_un = 25 - settlement_un - len(str(user_cart[1])) - len(user_cart[0])
consumption += user_cart[1]*number
print(" "*5,user_cart[0],"."*settlement_un,user_cart[1],"¥","."*to_settlement_un,"%s个"%(number))
times = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
print(times.center(50,"-"))
print("Shopping cost \033[32m%s\033[0m¥, Current balance \033[32m%s\033[0m¥,Thank you to patronize!".center(50,"-")%(consumption,user_in_money))
exit() if __name__ == "__main__":
in_money()

购物车

python练习_购物车(简版)的更多相关文章

  1. python练习_购物车(2)

    用python写了一个购物车程序,主要是练习,代码如下 主入口文件:main.py #!/usr/bin/env python # -*- coding:utf-8 -*- #先调用用户登录函数,在进 ...

  2. python学习_新闻联播文字版爬虫(V 1.0版)

    python3的爬虫练习,爬取的是新闻联播文字版网站 #!/usr/bin/env python # -*- coding: utf-8 -*- ''' __author__ = 'wyf349' _ ...

  3. Python练习_购物车_day6

    第一次代码 (1) 输出商品列表,用户输入序号,显示用户选中的商品. 页面显示 序号 + 商品名称,如: 1 手机 2 电脑 (2): 用户输入选择的商品序号,然后打印商品名称 (3):如果用户输入的 ...

  4. python学习_新闻联播文字版爬虫(V 1.0.1版)

    更新记录: 1.新增了headers头的随机获取: 2.新增了logging模块添加日志信息(学习用): #!/usr/bin/env python # -*- coding: utf-8 -*- ' ...

  5. 按行切割大文件(linux split 命令简版)

    按行切割大文件(linux split 命令简版) #-*- coding:utf-8 -*- __author__ = 'KnowLifeDeath' ''' Linux上Split命令可以方便对大 ...

  6. Underscore源码阅读极简版入门

    看了网上的一些资料,发现大家都写得太复杂,让新手难以入门.于是写了这个极简版的Underscore源码阅读. 源码: https://github.com/hanzichi/underscore-an ...

  7. python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学

    首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...

  8. 《利用Python进行数据分析·第2版》第四章 Numpy基础:数组和矢量计算

    <利用Python进行数据分析·第2版>第四章 Numpy基础:数组和矢量计算 numpy高效处理大数组的数据原因: numpy是在一个连续的内存块中存储数据,独立于其他python内置对 ...

  9. < 利用Python进行数据分析 - 第2版 > 第五章 pandas入门 读书笔记

    <利用Python进行数据分析·第2版>第五章 pandas入门--基础对象.操作.规则 python引用.浅拷贝.深拷贝 / 视图.副本 视图=引用 副本=浅拷贝/深拷贝 浅拷贝/深拷贝 ...

随机推荐

  1. android4.0 的图库Gallery2代码分析(二)

    最近迫于生存压力,不得不给人兼职打工.故在博文中加了个求点击的链接.麻烦有时间的博友们帮我点击一下.没时间的不用勉强啊.不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺.嘻嘻.http: ...

  2. ural1494 Monobilliards

    Monobilliards Time limit: 1.0 secondMemory limit: 64 MB A monobilliards table set up in a gaming hou ...

  3. rsync+inotify实现数据的实时备份

    一.rsync概述 1.1.rsync的优点与不足 rsync与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需 ...

  4. word采用尾注进行参考文献排版的一些问题

    使用Word中尾注的功能可以很好地解决论文中参考文献的排序问题.方法如下: 1.光标移到要插入参考文献的地方,菜单中“插入”——“引用”——“脚注和尾注”. 2.对话框中选择“尾注”,编号方式选“自动 ...

  5. The 2014 ACMICPC Asia Regional Guangzhou Online

    [A]-_-/// [B]线段树+位运算(感觉可出) [C]地图BFS,找最长线 [D]地图BFS,加上各种复杂情况的最短路-_- [E]-_-/// [F]三分+圆与线段的交点,计算几何 [G]-_ ...

  6. CodeForces 621C Wet Shark and Flowers

    方法可以转化一下,先计算每一个鲨鱼在自己范围内的数能被所给素数整除的个数有几个,从而得到能被整除的概率,设为f1,不能被整除的概率设为f2. 然后计算每相邻两只鲨鱼能获得钱的期望概率,f=w[id1] ...

  7. iOS进阶

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:wjh2005链接:https://www.zhihu.com/question/28518265/answer/887505 ...

  8. “canvas画布仿window系统自带画图软件"项目的思考

    "canvas画布仿window系统自带画图软件"项目的思考 首先贴上DEMO图,并没有美化效果.对UI有要求的,请自带补脑技术. 思考一 在做项目的过程中,我发现"工具 ...

  9. 《javascript语言精粹》——第3章对象

    第三章:对象: 属性名字:可以是包括空字符串在内的任意字符串: 属性值:是除undefined值之外的任何值; [1].对象字面量: var obj={}; //空对象 var obj = new O ...

  10. X-002 Exyson4412芯片启动过程分析

    移植u-boot到FriendlyARM Tiny4412开发板上,首先我们需要对Samsung Exyson4412芯片的启动方式.系统时钟初始化.串口初始化.内存初始化以及开发板的内存地址空间分配 ...