# -*- 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练习之购物车脚本的更多相关文章

  1. python 暴力破解密码脚本

    python 暴力破解密码脚本 以下,仅为个人测试代码,环境也是测试环境,暴力破解原理都是一样的, 假设要暴力破解登陆网站www.a.com 用户 testUser的密码, 首先,该网站登陆的验证要支 ...

  2. 一个好用的Python备份mysql的脚本

    前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...

  3. 用Python写的简单脚本更新本地hosts

    这两天Google墙得严重,于是就产生了做个一键更新hosts的脚本的想法. 由于正在学习Python,理所当然用Python来写这个脚本了. 接触比较多的就是urllib2这个库,习惯性的impor ...

  4. 现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的

    现在都是python 单独开发框架 执行脚本,处理结果,发报告之类的

  5. python练习_购物车(简版)

    python练习_购物车(简版) 需求: 写一个python购物车可以输入用户初始化金额 可以打印商品,且用户输入编号,即可购买商品 购物时计算用户余额,是否可以购买物品 退出结算时打印购物小票 以下 ...

  6. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  7. Python 网站后台扫描脚本

    Python  网站后台扫描脚本 #!/usr/bin/python #coding=utf-8 import sys import urllib import time url = "ht ...

  8. python简单的监控脚本-利用socket、psutil阻止远程主机运行特定程序

    python简单的监控脚本-利用socket.psutil阻止远程主机运行特定程序 psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取 ...

  9. python编写文件统计脚本

    python编写文件统计脚本 思路:用os模块中的一些函数(os.listdir().os.path.isdir().os.path.join().os.path.abspath()等) 实现功能:显 ...

随机推荐

  1. Machine Learning Algorithms Study Notes(1)--Introduction

    Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    ...

  2. NOIP2009靶形数独[DFS 优化]

    描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z博士请教,Z 博士拿出了他最近发明的“靶形数独 ...

  3. Unity4.3 bug GetChild顺序错乱

    历史原因,目前有个项目还在使用unity4.3版本,比较过不同Unity版本,发现unity4.3的 transform.GetChild 获取的child顺序并不是想要的. 测试代码 using U ...

  4. Android驱动入门-LED--HAL硬件访问服务层②

    硬件平台: FriendlyARM Tiny4412 Cortex-A9 操作系统: UBUNTU 14.04 LTS 时间:2016-09-21  16:58:56 为了避免访问冲突,则创建了硬件访 ...

  5. win2008server R2 x64 部署.net core到IIS上出现【Failed to load the dll from [C:\Program Files\dotnet\host\fxr\1.0.1\hostfxr.dll], HRESULT: 0x80070057】错误

    win2008server R2 x64 部署.net core到IIS上出现[Failed to load the dll from [C:\Program Files\dotnet\host\fx ...

  6. js点击左右滚动+默认自动滚动类

    js点击左右滚动+默认自动滚动类 点击下载

  7. curl命令

    定位后端接口是否ok,经常使用到curl -b/cookie  <name=string/file> cookie字符串或文件读取位置 curl http://localhost --co ...

  8. win7登录后开机密码破解读取

    在win7登录后,win7密码可以直接读取. https://github.com/gentilkiwi/mimikatz

  9. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  10. githup上传代码

    把自己本地东西上传到GitHup上. 本文内容来自于http://blog.csdn.net/yuanzichao/article/details/44922593 1.安装msysgit和Torto ...