Talk is cheap,Let's do this!

product_list = [
['Iphone7 Plus', 6500],
['Iphone8 ', 8200],
['MacBook Pro', 12000],
['Python Book', 99],
['Coffee', 33],
['Bike', 666],
['pen', 2]
]
shopping_cart = [] f = open('user.txt', 'r')
lock_file = f.readlines()
f.close()
count = 0
user_list = {} while True:
if count == 3:
print("用户名输入次数到达3次限制")
break
for i in lock_file:
i = i.strip()
user_list[i.split('|')[0]] = {'password': i.split('|')[1]}
user_name = input("请输入您的用户名>>:")
if user_name not in user_list:
print("用户名错误")
count += 1
if user_name in lock_file:
print("用户名已锁定,请联系管理员!")
exit()
if user_name in user_list:
user_password = input("请输入您的密码>>: ")
if user_password == user_list[user_name]['password']:
print("欢迎登录电子商城")
while True:
salary = input("请输入您的工资:") # 输入金额
if not salary.isdigit(): # 判断输入的salary是不是数字
print("由于您的输入的工资不合法,请再次输入金额") # 输入金额不合法
continue
else:
salary = int(salary) # 把输入的数字转成整形
break
while True:
print(">> 欢迎来到电子商城 <<")
for index, i in enumerate(product_list): # 循环商品列表,商品列表索引
print("%s.\t%s\t%s" % (index, i[0], i[1])) # 打印商品列表,显示商品列表索引
choice = input(">>请输入商品序号或输入 exit 退出商城>>: ").strip()
if len(choice) == 0: # 判断输入字符串是否为空和字符串长度
print('-->您没有选择商品<--')
continue
if choice.isdigit(): # 判断输入的choice是不是一个数字
choice = int(choice) # 把输入的字符串转成整型
if choice < len(product_list) and choice >= 0: # 输入的整数必须小于商品列表的数量
product_item = product_list[choice] # 获取商品
if salary >= product_item[1]: # 拿现有金额跟商品对比,是否买得起
salary -= product_item[1] # 扣完商品的价格
shopping_cart.append(product_item) # 把选着的商品加入购物车
print("添加 \033[32;1m%s\033[0m 到购物车,您目前的金额是 \
\033[31;1m%s\033[0m" % (product_item[0], salary))
else:
print("对不起,您的金额不足,还差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
else:
print("-->没有此商品<--")
elif choice == "exit":
total_cost = 0
print("您的购物车列表:")
for i in shopping_cart:
print(i)
total_cost += i[1]
print("您的购物车总价是: \033[31;1m%s\033[0m" % (total_cost,))
print("您目前的余额是:\033[31;1m%s\033[0m" % (salary,))
break
break
else:
print("密码错误")
count += 1
if count == 3:
print("您输入的密码错误次数已达3次,将锁定您的用户!")
f = open('blacklist.txt', 'w')
f.write('%s' % user_name)
f.close()
break while True:
salary = input("请输入您的工资:") # 输入金额
if not salary.isdigit(): # 判断输入的salary是不是数字
print("由于您的输入的工资不合法,请再次输入金额") # 输入金额不合法
continue
else:
salary = int(salary) # 把输入的数字转成整形
break
while True:
print(">> 欢迎来到电子商城 <<")
for index, i in enumerate(product_list): # 循环商品列表,商品列表索引
print("%s.\t%s\t%s" % (index, i[0], i[1])) # 打印商品列表,显示商品列表索引
choice = input(">>请输入商品序号或输入 exit 退出商城>>: ").strip()
if len(choice) == 0: # 判断输入字符串是否为空和字符串长度
print('-->您没有选择商品<--')
continue
if choice.isdigit(): # 判断输入的choice是不是一个数字
choice = int(choice) # 把输入的字符串转成整型
if choice < len(product_list) and choice >= 0: # 输入的整数必须小于商品列表的数量
product_item = product_list[choice] # 获取商品
if salary >= product_item[1]: # 拿现有金额跟商品对比,是否买得起
salary -= product_item[1] # 扣完商品的价格
shopping_cart.append(product_item) # 把选着的商品加入购物车
print("添加 \033[32;1m%s\033[0m 到购物车,\
您目前的金额是 \033[31;1m%s\033[0m" % (product_item[0], salary))
else:
print("对不起,您的金额不足,还差 \033[31;1m%s\033[0m" % (product_item[1] - salary,))
else:
print("-->没有此商品<--")
elif choice == "exit":
total_cost = 0
print("您的购物车列表:")
for i in shopping_cart:
print(i)
total_cost += i[1]
print("您的购物车总价是: \033[31;1m%s\033[0m" % (total_cost,))
print("您目前的余额是: \033[31;1m%s\033[0m" % (salary,))
break

Python入门之实现简单的购物车功能的更多相关文章

  1. Python入门一:简单得不能再简单了##

    从python的语法上看,简单得不能再简单了. 想学它,请移步廖雪峰python2.7教程以及python3.这实在是最好的入门教程.参考资料太多: 外国的教程 Python 入门指南 Python ...

  2. 大爽Python入门教程 1-1 简单的数学运算

    大爽Python入门公开课教案 点击查看教程总目录 1 使用pycharm建立我们的第一个项目 打开pycharm,点击菜单栏,File->New Project 在Location(项目地址) ...

  3. 大爽Python入门教程 1-3 简单的循环与判断

    大爽Python入门公开课教案 点击查看教程总目录 这里只初步认识下循环和判断,以便于我们去实现一些简单的计算. 循环和判断的详细知识和细节,我们将在后面的章节(大概是第三章)展开阐述. 1 初步了解 ...

  4. jQuery使用cookie与json简单实现购物车功能

    本文实例讲述了jQuery使用cookie与json简单实现购物车的方法.分享给大家供大家参考,具体如下: 1.生成一个cookie 用来存储商品的id  String类型 2.添加商品id的时候 把 ...

  5. python入门:模拟简单用户登录(自写)

    #!/usr/bin/env python # -*- coding: utf-8 -*- #模拟简单用户登录(自写) import getpass a = raw_input("Pleas ...

  6. 利用Vue实现一个简单的购物车功能

    开始学习Vue的小白,dalao多多指正 想要实现下面这个界面,包含总价计算.数量增加和移除功能 话不多说代码如下 <!DOCTYPE html> <html> <hea ...

  7. Python实战之实现简单的购物车系统

    #!usr/bin/env Python3 # -*-coding:utf-8-*- # 程序:购物车程序 # # 需求: # # 启动程序后,让用户输入工资,然后打印商品列表 # 允许用户根据商品编 ...

  8. vuex——做简单的购物车功能

    购物车组件 <template> <div> <h1>vuex-shopCart</h1> <div class="shop-listb ...

  9. vue实现简单的购物车功能

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Asp.net MVC]Asp.net MVC5系列——Routing特性

    目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...

  2. 51nod1126 求递推序列的第N项

    求递推序列的第N项 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的 ...

  3. 反正切函数atan与atan2的区别

    atan 和 atan2 都是求反正切函数,如:有两个点 point(x1,y1), 和 point(x2,y2); 那么这两个点形成的斜率的角度计算方法分别是: float angle = atan ...

  4. ubuntu16.04下安装opencv3.1.0

    1.安装依赖项 sudo apt--dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 可选的 sudo apt--dev lib ...

  5. Mapreduce 原理及程序分析

    1.MapReduce(Map+Reduce) 提出一个问题: 目标:你想数出一摞牌中有多少张黑桃. 直观方式:一张一张检查并且数出有多少张是黑桃数目 MapReduce方法则是: 给在座的所有玩家中 ...

  6. ddt ddl dml

    data-definition language 数据定义语言 data-manipulation language 数据操纵语言 data type definition 文档类型定义

  7. HTML标签_head标签

    HTML标签分为两种,自闭合标签和主动闭合标签:没有另一半 自动闭合标签,有另一半叫主动闭合标签. 自动闭合标签,只有开头没有结尾,即没有另一半:如<meta charset="UTF ...

  8. 20144306《网络对抗》Web安全基础实践

    1  实验内容 SQL注入攻击 XSS攻击 CSRF攻击 2  实验过程记录 2.1WebGoat说明与安装 关于WebGoat WebGoat是OWASP组织研制出的用于进行web漏洞实验的应用平台 ...

  9. 钱币兑换问题--hdu1284(完全背包)

    Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法.   Input 每行只有一个正整数N,N小于32768.   ...

  10. PHPExcel 基本用法详解

    .header header("Content-Type:application/vnd.ms-excel"); header("Content-Disposition: ...