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()等) 实现功能:显 ...
随机推荐
- hdu-5127------hdu5137
hdu-5127 思路: 本来正解好像是动态凸包,暴力10000+ms可以搞过去; hdu-5128 思路: 枚举两个长方形的对角线,然后判断是否不相交,更新答案就好; hdu-5130 思路: 将题 ...
- AC日记——逆波兰表达式 openjudge 3.3 1696
1696:逆波兰表达式 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算符前置的算术表达式,例如普通的表达式2 + 3的逆波兰表示法为+ 2 3.逆波兰表达式 ...
- AC日记——石头剪子布 openjudge 1.7 04
04:石头剪子布 总时间限制: 1000ms 内存限制: 65536kB 描述 石头剪子布,是一种猜拳游戏.起源于中国,然后传到日本.朝鲜等地,随着亚欧贸易的不断发展它传到了欧洲,到了近现代逐渐风 ...
- 父元素与子元素之间的margin-top问题
父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. html代码: <div c ...
- QuickFIX/N 动态存储配置信息
Acceptor或者Initiator能够为您维护尽可能多的FIX会话,因而FIX会话标识的唯一性非常重要.在QuickFIX/N中,一个FIX会话的唯一标识是由:BeginString(FIX版本号 ...
- java 27 - 5 反射之 通过反射获取成员方法并使用
类Method:提供关于类或接口上单独某个方法(以及如何访问该方法)的信息. A:获取所有方法 数组 1.getMethods 获取该类包括其父类的公共成员方法 2.getDeclaredMetho ...
- rpc使用举例
#server.py from SimpleXMLRPCServer import SimpleXMLRPCServer def add(x,y): return x+y server=SimpleX ...
- redis-cache中的callback
这个是mybatis/redis-cache中关键类 RedisCache 的源码 /** * Copyright 2015 the original author or authors. * * ...
- 在Java代码中使用iTextPDF生成PDF
1. 生成PDF 载入字体 static { FontFactory.register("/fonts/msyh.ttf"); FontFactory.register(" ...
- jquery 时间运算、格式化的方法扩张
/* 函数:日期 加n天 参数:n是天数 返回:n天后的日期 */ Date.prototype.addDays = Date.prototype.addDays || function (n) { ...