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()等) 实现功能:显 ...
随机推荐
- iScroll4下表单元素聚焦及键盘的异常问题
本文是zawa同事写的一篇博文,相信很多在webapp开发中的同学使用iscroll4会遇到的该问题,问过zawa兄的建议,在这里分享给大家,希望能帮助到各位~ 原文地址:http://www.zaw ...
- 【原】webapp开发中兼容Android4.0以下版本的css hack
话说现在的手机型号越来越多,主要还是android和ios这2个巨头称霸了江湖,而他们自带的浏览器内核是webkit,那对于做移动网页开发的同事来说,一般只要做好webkit内核浏览器的展现效果就行了 ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- GTAC 2015 Schedule
之前发的GTAC 2015将于11月10号和11号召开 现在时刻表也出来啦 https://developers.google.com/google-test-automation-conferenc ...
- 第27章 结构型模式大PK
27.1 代理模式 VS 装饰模式 27.1.1 代理模式 (1)场景:客人找运动员代理要求安排运动员参加比赛 (2)说明:代理人有控制权,可以拒绝客人的要求,也可以答应安排,甚至自己下去跑(因为有些 ...
- URL和URI区别
URI:Universal Resource Identifier,通用资源标识符: URL:Uniform Resource Locator,统一资源定位符: 其中,URL是URI ...
- Unity 碰撞器
Unity引擎的碰撞器 常见的有Box Collider,Capsule Collider等等 IsTrigger(触发器) 在这些碰撞器中有一个属性 Is Trigger(是否使用触发器) 触发器事 ...
- easyUI的dateBox控制时间格式
<input type='text' name='yearQuery' class='easyui-datebox ' data-options="formatter:myformat ...
- php一句话后门过狗姿势万千之传输层加工(1)
写在前面:过狗相关的资料网上也是有很多,所以在我接下来的文章中,可能观点或者举例可能会与网上部分雷同,或者表述不够全面.但是我只能说,我所传达给大家的信息,是我目前所掌握或者了解的,不能保证所有人都会 ...
- MVC code first数据迁移 转
coptto:http://www.cnblogs.com/miro/p/4164076.html 本篇是相对独立的一篇,主要讲解不丢失数据进行数据库结构升级. 前面我们讲解EF功能时(见第三篇文章) ...