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()等) 实现功能:显 ...
随机推荐
- 我的Github之旅(一)
第一站:本地环境中的Github配置 1.参考链接 作为初学者,需要了解的有[本地环境中的github配置(基于mac)][1],以及git知识,这里推荐一个网站[猴子都能懂的Git入门][2],最后 ...
- python中if __name__ == "__main__":用法解析
__name__: __name__作为模块的内置属性,简单点说呢,就是.py文件的调用方式. __main__: 如果__name__等于"__main__"就表示是直接执行. ...
- VS使用Sublime Text 主题
VS主题需求 VS默认有三种主题Dark,Light,Blue,但你是否想寻找其它的主题呢?本文介绍两种方法来满足你的不同需求 1.微软官方扩展 微软官方推出的 Visaul Studio Color ...
- Unity减少GC Alloc之 使用for替换foreach
Unity中foreach会增加GC unity中for效率比foreach高? 在unity中使用foreach遍历集合会增加gc alloc,参考的话题:作为Unity3D的脚本而言,c#中for ...
- 理解Java中字符流与字节流的区别
1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...
- noip模拟赛(10.4) 字典序(dictionary)
[题目描述] 你需要构造一个1~n的排列,使得它满足m个条件,每个条件形如(ai,bi),表示ai必须在bi前面.在此基础上,你需要使它的字典序最小. [输入数据] 第一行两个正整数n,m.接下来m行 ...
- HTML DOM 属性 对象
HTML DOM 属性 对象 HTML DOM 节点 在 HTML DOM (Document Object Model) 中, 所有的都是 节点: 文档是文档节点 所有 HTML 元素是元素节点 所 ...
- BZOJ 2005: [Noi2010]能量采集
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 3312 Solved: 1971[Submit][Statu ...
- Resample the mask
我们所用功能像和mask的size不同时,我们首先要对mask进行resample,令其和功能像的size相同才可以. 根据严超赣老师的回复,有三种方法:http://restfmri.net/for ...
- MySql MyISAM和InnoDB的区别
MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写,它是存储记录和文件的标准方法. ...