python练习之购物车脚本
# -*- coding:utf-8 -*-
#简单的购物车小程序 author:李瑞鑫
shopping_car =[]
product_list_tatol = "---product list----"
welcome = "-----------welcome to shopping marketi----------"
product_list = [
('iphone',5888),
('lenovo',998),
('cup',12),
('thinkpad',4500),
('notebook',6)
]
print welcome
salary = raw_input("input your salary:>>>")
if salary.isdigit():
salary = int(salary)
while True:
print product_list_tatol
for item in product_list:
print product_list.index(item)+1,item
choice = raw_input("choice you want to buy:>>> ___ 0 to exit")
if choice.isdigit():
choice = int(choice)
if choice > 5 or choice < 0:
print "no this goods,请重新选择"
continue
elif choice <=5 and choice >=1:
if salary < product_list[choice-1][1]:
print "账户余额不足,请购买其他商品或者退出"
continue
else:
pass
item_choice = product_list[choice-1]
shopping_car.append(item_choice)
print "you buy goods is", shopping_car
salary = salary - product_list[choice-1][1]
print "your balance is %d"%(salary) elif choice == 0:
print "you buy goods is",shopping_car,
print "your balance is %d"%(salary)
exit()
2.带有身份认证的购物车程序
# -*- coding:utf-8 -*-
import time, os
shop_car3 = []
shop_car2 = []
user_list = []
count = 0
user = "liruixin"
password = ""
raw_user = raw_input("username:")
f = open("user.txt", 'a+')
lines = []
for lines in f.readlines():
lines = lines.replace("\n", "").split(",")
f.close()
for i in lines:
if i == raw_user:
print "this user is locked, please request to root"
exit()
else:
pass
while count < 3:
if raw_user == user:
raw_password = raw_input("password:")
count = count + 1
if count == 3:
user_list.append(raw_user)
user = open('user.txt', 'a')
for raw_user in user_list:
user.write(raw_user + ',')
user.close()
else:
pass
if raw_password == password:
print "success"
a = time.localtime()
b = time.strftime("%Y-%m-%d %H:%M:%S", a)
title = "welcome to shopping"
print b.center(40, "*")
print title.center(40, "*")
shop_car = []
goods_list = [
["iphone 7", 5888],
["coffe", 30],
["computer", 3999],
["mac pro", 12000]
]
filename = r'salary.txt'
if os.path.exists(filename):
temp = open("salary.txt", "r")
for i in temp:
salary = i
print "您的卡上余额为", salary
else:
salary = raw_input("请输入充值金额:") if salary.isdigit():
salary = int(salary)
while True:
print "******* good list **********"
for i in enumerate(goods_list):
print i[0] + 1, i[1][0], i[1][1]
choice = raw_input("choice goods __ quit to exit:")
if choice.isdigit():
choice = int(choice)
if choice <= 4 and choice >= 1:
if salary >= goods_list[choice - 1][1]:
salary = salary - goods_list[choice - 1][1]
price = (goods_list[choice - 1][1])
item = (goods_list[choice - 1][0])
print "你购买了 [\033[32;1m%s\033[0m] 花费了 [\033[31;1m%d\033[0m] , 你的余额为 [\033[31;1m%d\033[0m]" % (
item, price, salary)
shop_car.append(item)
shop_car2.append(price)
else:
print "你的余额不足,请选择其他商品"
continue
elif choice == "quit":
print "你购买的商品如下"
a = set(shop_car)
for i in a:
print i,shop_car.count(i)
b = sum(shop_car2)
print "总共消费了 \033[32;1m%d\033[0m 元钱"%b
print "你的余额为\033[31;1m%d\033[0m 元" % salary
print "欢迎下载再来,拜拜"
salary_file = open("salary.txt", "w")
salary_file.write("%d" % salary)
salary_file.close()
exit()
else:
print "没有该商品,请重新输入"
continue
else:
print "this password error"
else:
print "no this user"
exit()
3.带有充值功能的购物车
# -*- coding:utf-8 -*-
import time, os
shop_car3 = []
shop_car2 = []
user_list = []
yue = []
count = 0
user = "liruixin"
password = ""
message = "欢迎光临天猫超市,请登录"
print message.center(50,"*")
raw_user = raw_input("username:")
f = open("user.txt", 'a+')
lines = []
for lines in f.readlines():
lines = lines.replace("\n", "").split(",")
f.close()
for i in lines:
if i == raw_user:
print "this user is locked, please request to root"
exit()
else:
pass
while count < 3:
if raw_user == user:
raw_password = raw_input("password:")
count = count + 1
if count == 3:
user_list.append(raw_user)
user = open('user.txt', 'a')
for raw_user in user_list:
user.write(raw_user + ',')
user.close()
else:
pass
if raw_password == password:
pass a = time.localtime()
b = time.strftime("%Y-%m-%d %H:%M:%S", a)
title = "登录成功,正在跳转"
print b.center(40, "*")
print title.center(40, "*")
shop_car = []
goods_list = [
["iphone 7", 5888],
["coffe", 30],
["computer", 3999],
["mac pro", 12000]
]
filename = r'salary.txt'
if os.path.exists(filename):
temp = open("salary.txt", "r")
for i in temp:
salary = int(i) print "您的卡上余额为", salary
chongzhi = raw_input("按a充值,按b进入商城")
if chongzhi =="b":
pass
if chongzhi =="a":
jine = int(raw_input("请输入充值金额:"))
sums = int(i)+jine
salary = sums
print "充值成功,最新余额为 \033[32;1m%d\033[0m "%sums
g = open("salary.txt","w")
g.write(str(sums))
g.close()
else:
salary = raw_input("请输入充值金额:") if salary.isdigit():
salary = int(salary)
while True:
print "******* good list **********"
for i in enumerate(goods_list):
print i[0] + 1, i[1][0], i[1][1]
choice = raw_input("choice goods __ quit to exit:")
if choice.isdigit():
choice = int(choice)
if choice <= 4 and choice >= 1:
if salary >= goods_list[choice - 1][1]:
salary = salary - goods_list[choice - 1][1]
price = (goods_list[choice - 1][1])
item = (goods_list[choice - 1][0])
print "你购买了 [\033[32;1m%s\033[0m] 花费了 [\033[31;1m%d\033[0m] , 你的余额为 [\033[31;1m%d\033[0m]" % (
item, price, salary)
shop_car.append(item)
shop_car2.append(price)
else:
rr = raw_input("你的余额不足,按y充值:按q返回商品列表")
if rr == "y":
r = int(raw_input("请输入充值金额:"))
summ = int(salary)+r
print "充值成功,最新余额 \033[32;1m%d\033[0m"%summ
salary = summ
o = open("salary.txt","w")
o.write(str(summ))
o.close()
elif rr =="q":
continue elif choice == "quit":
print "你购买的商品如下"
a = set(shop_car)
for i in a:
print i,shop_car.count(i)
b = sum(shop_car2)
print "总共消费了 \033[32;1m%d\033[0m 元钱"%b
print "你的余额为\033[31;1m%d\033[0m 元" % salary
print "欢迎下次再来,拜拜"
salary_file = open("salary.txt", "w")
salary_file.write("%d" % salary)
salary_file.close()
exit()
else:
print "没有该商品,请重新输入"
continue
else:
print "this password error"
else:
print "no this user"
exit()
python练习之购物车脚本的更多相关文章
- python 暴力破解密码脚本
python 暴力破解密码脚本 以下,仅为个人测试代码,环境也是测试环境,暴力破解原理都是一样的, 假设要暴力破解登陆网站www.a.com 用户 testUser的密码, 首先,该网站登陆的验证要支 ...
- 一个好用的Python备份mysql的脚本
前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...
- 用Python写的简单脚本更新本地hosts
这两天Google墙得严重,于是就产生了做个一键更新hosts的脚本的想法. 由于正在学习Python,理所当然用Python来写这个脚本了. 接触比较多的就是urllib2这个库,习惯性的impor ...
- 现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的
现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的
- python练习_购物车(简版)
python练习_购物车(简版) 需求: 写一个python购物车可以输入用户初始化金额 可以打印商品,且用户输入编号,即可购买商品 购物时计算用户余额,是否可以购买物品 退出结算时打印购物小票 以下 ...
- 0003.5-20180422-自动化第四章-python基础学习笔记--脚本
0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...
- Python 网站后台扫描脚本
Python 网站后台扫描脚本 #!/usr/bin/python #coding=utf-8 import sys import urllib import time url = "ht ...
- python简单的监控脚本-利用socket、psutil阻止远程主机运行特定程序
python简单的监控脚本-利用socket.psutil阻止远程主机运行特定程序 psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取 ...
- python编写文件统计脚本
python编写文件统计脚本 思路:用os模块中的一些函数(os.listdir().os.path.isdir().os.path.join().os.path.abspath()等) 实现功能:显 ...
随机推荐
- 关于PHP上传文件和中文名乱码情况
关于PHP文件上传 在前端HTML页面,表单如下 Upload.html <!doctype html><html lang="en"><head&g ...
- linux下内网端口转发工具:linux版lcx [实现远程内网维护]
这个工具以前使用的初衷是内网渗透,需要将内网ssh端口转发到外网服务器上.但这个工具同样适用于运维工程师进行远程内网维护. 当然这一切的前提是内网可以访问外网,检测方法当然就是直接ping 一个外网I ...
- ip的划分,超详细
from:http://blog.liuts.com/post/128/ IP和子网掩码我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.2 ...
- HTML5+jquery整屏页面切换效果
压缩包下载 演示地址 http://www.yyyweb.com/demo/page-transitions/
- 高端黑链SEO—恶意JS脚本注入访问伪随机域名
摘要:我们的服务器又出入侵事故了.有客户的 html 网页底部被插入了一段 js 脚本,导致访客打开网页时被杀毒软件警告网站上有恶意代码.在黑链 SEO 中这是常见的手法,但奇特的地方就在于我们这次捕 ...
- Openjudge 1.13-40 提取数字串按数值排序
40:提取数字串按数值排序 查看 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个字符串,请将其中的所有数字串提取,并将每个数字串作为整数看待(假设可以用int 表示),按从 ...
- wcf的诡异问题
最近在做工作流设计器用的silverlight 使用wcf服务. 过程中遇到了两个奇怪的问题. 1. 出现整型参数无法传递到服务器的情况 2.出现反序列化失败的情况. 经过仔细排查发现 每个属性都有一 ...
- shiro退出登陆清空缓存实现
上一篇介绍了使用springmvc集成shiro登陆过程(http://www.cnblogs.com/nosqlcoco/p/5579081.html),通过FormAuthenticationFi ...
- 使用Netty绑定一个端口如何分辨出多种类型的DTU的注册包
一. 背景 项目需要使用Netty和DTU(无线数据传输模块)通信,需要接入多种类型的DTU,每种dtu连接上来之后都首先会发送一个注册报文.需要解析该注册报文来实现: 1. 分辨出是哪种类型的dt ...
- python网络编程学习《一》
最近,刚实习完,很喜欢实验楼,但是自己的方向仍然不能确定,自己觉得可选择的空间很大,尽管已经是大四的人了,想到别人都在忙着买职业装,买高跟鞋面试,学习化妆什么的,看看自己,反而开始慢慢关注运动,食疗以 ...