作业:购物商城

商品展示,价格

买,加入购物车

付款,钱不够

代码如下:

import codecs
#登录接口,用户名密码都正确登录成功,否则失败 def login(your_name,your_password):
with open("user_file","r") as l:
for line in l:
user_name,user_password,user_balance=line.strip().split(":")
if your_name == user_name and your_password == user_password:
flag =
break
else:
print("用户名或密码错误")
flag =
return flag #商品展示列表
def show_goods():
print(*'*' + "商品列表" + *"*")
print( * "-")
print("| 编号 | 名称 | 价格 | 数量 | ")
try:
with codecs.open("goods_file","r","utf-8") as f1:
for line in f1:
(goods_id,goods_name,goods_rate,goods_num) = line.strip().split(":")
print( * "-")
print('| %s | %s | %s | %s |' %(goods_id,goods_name,goods_rate,goods_num))
print(*"-")
# print("购买商品请输入产品编号/返回请输入b/退出请输入q")
# the_num = input("请选择:")
except ValueError:
pass #展示购物车
def show_gouwuche(your_name):
with codecs.open("gouwuche","r","utf-8") as f2:
n =
print( * "-")
print("| 编号 | 名称 | 数量 | 总价 |")
for line in f2:
user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
if user_name == your_name:
n +=
print( * "-")
print(" | %s | %s | %s | %s |" %(n,goods_name,goods_num,goods_rate))
print(*"-") #查看当前用户已购产品
def show_yigou(your_name):
with codecs.open("yigoushangpin","r","utf-8") as f3:
n =
print(*"-")
print("| 编号 | 名称 | 数量 | 总价 |")
for line in f3:
user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
if user_name == your_name:
n +=
print(*"-")
print('| %s | %s | %s | %s |' %(n,goods_name,goods_num,goods_rate))
print(*"-") #账户余数查看
def show_money(your_name):
with open("user_file","r") as l:
for line in l:
user_name,user_password,user_blance = line.strip().split(":")
if your_name == user_name:
print("您当前账户余额为:%s" %user_blance)
break # 账户余额查看
def show_money(your_name):
with open('user_file', 'r') as l:
for line in l:
user_name, user_password, user_balance = line.strip().split(':')
if your_name == user_name:
print('您当前账户余额为:%s' % user_balance)
break #加入购物车,如果购物车中已经存在该商品则将商品数量相加,商品价格相加;如果购物车中不存在该商品,则添加到购物车中,并将可购买的库存量
def add_gouwuche(n,your_name):
with codecs.open("goods_file","r+","utf-8") as f1:
for line1 in f1:
goods_id,goods_name,goods_rate,goods_num = line1.strip().split(":")
if n == goods_id:
goods_quantity = input("请输入数量:")
#如果输入的并非数字则不合法,输入的数量大于库存,均需要重新输入
while True:
if goods_quantity.isdigit():
if int(goods_num) < int(goods_quantity):
goods_quantity = input("您输入的数量大于当前库克数量,请重新输入数量:")
else:
break
else:
goods_quantity = input("您输入的不合法,请重新输入数量:")
with codecs.open("gouwuche","r+","utf-8") as f2:
for line2 in f2:
che_user_name,che_goods_name,che_goods_num,che_goods_rate = line2.strip().split(":")
#判断当前选择的商品是否在购物车存在,如果存在标志位che_flag =
if your_name == che_user_name and goods_name == che_goods_name:
che_flag =
che_goods_num = str(int(che_goods_num) + int(goods_quantity))
che_goods_rate = str(int(che_goods_rate) + int(goods_rate)*int(goods_quantity))
old_line = line2
new_line = che_user_name + ":" + che_goods_name + ":" + che_goods_num + ":" + che_goods_rate + "\n"
break
#当前选择的商品购物车中不存在,标志位che_flag =
else:
che_flag =
che_goods_rate = str(int(goods_rate) * int(goods_quantity))
che_goods_num = goods_quantity
che_goods_name = goods_name
che_user_name = your_name
new_line = ("%s:%s:%s:%s\n" %(che_user_name,che_goods_name,che_goods_num,che_goods_rate))
#购物车中存在,则将购物车中的数量更新,总价格更新
if che_flag == :
f2.seek()
old_gouwuche = f2.read()
new_gouwuche = old_gouwuche.replace(old_line,new_line)
f2.seek()
f2.truncate()
f2.write(new_gouwuche)
#购物车中不存在,则在购物车中添加
elif che_flag == :
f2.seek()
old_gouwuche =f2.read()
new_gouwuche = old_gouwuche + new_line
f2.seek()
f2.truncate()
f2.write(new_gouwuche)
#加入购物车后,将商品列表中的数量更新
new_goods_num = str(int(goods_num) - int(goods_quantity))
old_goods_line = line1
new_goods_line = ("%s:%s:%s:%s\n" %(goods_id,goods_name,goods_rate,new_goods_num))
f1.seek()
old_goods_file = f1.read()
new_goods_file = old_goods_file.replace(old_goods_line,new_goods_line)
f1.seek()
f1.truncate()
f1.write(new_goods_file)
goods_flag = True
break
else:
goods_flag = False
print("抱歉您选择的商品不存在,请重新选择")
return goods_flag #删除购物车的商品
def del_gouwuche(del_gouwuche_num,your_name):
with codecs.open("gouwuche","r+","utf-8") as f1:
while True:
n =
for line in f1:
user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
#判断删除的商品是否存在,若存在撒谎拿出标志位del_flag =
if user_name == your_name:
n +=
if int(del_gouwuche_num) == n:
del_goods_name = goods_name
del_goods_num = goods_num
del_goods_line = line1
del_flag =
break
#商品存在,进行删除
if del_flag == :
f1.seek()
gouwuche_file = f1.readlines()
gouwuche_file.remove(del_goods_line)
f1.seek()
f1.truncate()
for line in gouwuche_file:
f1.write(line)
#更新商品清单,将从购物车删除的加到商品清单中
with codecs.open("goods_file","r+","utf-8") as f2:
for line2 in f2:
old_goods_line = list(line2.strip().split(":"))
old_line = line2.strip()
if old_goods_line[] == del_goods_name:
old_goods_line[] = str(int(old_goods_line[])) + int(del_goods_num)
new_goods_line = old_goods_line
del_flag =
break
if del_flag == :
f2.seek()
new_line = ":".join(new_goods_line)
old_goods_file = f2.read()
new_goods_file = old_goods_file.replace(old_line,new_line)
f2.seek()
f2.truncate()
f2.write(new_goods_file)
break
#要删除的商品不存在,标志位del_flag =
else:
del_flag =
print("您要删除的商品不存在,请重新输入编号!")
break
return del_flag #支付购物车商品
def pay_gouwuche(gouwuche_num,your_name):
with codecs.open("gouwuche","r+","utf-8") as f1:
while True:
n =
for line1 in f1:
user_name,goods_name,goods_num,goods_rate = line1.strip().split(":")
#判断删除的商品是否存在,若存在删除标志位del_flag =
if user_name == your_name:
n +=
if int(gouwuche_num) == n:
pay_goods_name = goods_name
pay_goods_num = goods_num
pay_goods_rate = goods_rate
pay_goods_line = line1
#查看当前用户余额是否能够支付商品
with open("user_file","r+") as user_file:
for line in user_file:
user_name,user_password,user_balance = line.strip().split(":")
if your_name == user_name:
old_balance_line = line
new_balance = int(user_balance) - int(goods_rate)
break
if new_balance < :
print("您的当前预余额不足,请充值后再支付!\n")
pay_flag = False
break
#大于等于0时,则可以支付,并更新账户余额
else:
pay_flag = True
new_balance_line = user_name + ":" + user_password + ":" + str(new_balance) + "\n"
user_file.seek()
old_user_file = user_file.read()
new_user_file = old_user_file.replace(old_balance_line,new_balance.line)
user_file.seek()
user_file.truncate()
user_file.write(new_user_file)
break
else:
pay_flag = ""
#商品存在,支付成功后进行删除
if pay_flag == True:
f1.seek()
gouwuche_file = f1.readlines()
gouwuche_file.remove(pay_goods_line)
f1.seek()
f1.truncate()
for line in gouwuche_file:
f1.write(line)
#更新已购买的商品清单,将已支付的商品加入到购买的清单中
with codecs.open("yigoushangpin","r+","utf-8") as f2:
for line2 in f2:
old_yigou_line = list(line2.strip().split(":"))
old_line = line2.strip()
#当已购商品清单存在该商品购买记录则将数量相加,总价格相加
if old_yigou_line[] == pay_goods_name and old_yigou_line[] == your_name:
old_yigou_line[] = str(int(old_yigou_line[]) + int(pay_goods_num))
old_yigou_line[] = str(int(old_yigou_line[]) + int(pay_goods_rate))
new_yigou_line = old_yigou_line
f2.seed()
new_line = ":".join(new_yigou_line)
old_yigou_file = f2.read()
new_yigou_file = old_yigou_file.replace(old_line,new_line)
f2.seed()
f2.truncate()
f2.write(new_yigou_file)
break
#当已购商品清单灭有购买记录,则附加购买记录
else:
f2.seek()
old_yigou_file = f2.read()
new_yigou_file = old_yigou_file + pay_goods_line
f2.seek()
f2.truncate()
f2.write(new_yigou_file)
print("\n")
print("恭喜你,支付成功!")
break
elif pay_flag == False:
flag = True
break
#要删除的商品不存在,标志位del_flag =
else:
pay_flag = False
print("您要支付的商品不存在,请重新输入编号!")
break
return pay_flag #账户充值
def add_money(blance_num,your_name):
with open("user_file","r+") as f:
for line in f:
user_name,user_passwd,user_balance = line.strip().split(":")
if user_name == your_name:
old_user_line = line
new_user_balance = str(int(user_balance) + int(balance_num))
new_user_line = user_name + ":" + user_passwd + ":" + new_user_balance + "\n"
break
f.seek()
old_user_file = f.read()
new_user_file = old_user_file.replace(old_user_line,new_user_line)
f.seek()
old_user_file = f.read()
new_user_file = old_user_file.replace(old_user_line,new_user_line)
f.seek()
f.truncate()
f.write(new_user_file) def main():
your_name = input("请输入用户名:")
your_password = input("请输入密码:")
menu_num = ""
#登录,用户名或密码错误重新输入
while True:
if login(your_name,your_password):
print("欢迎%s登录购物商城" %your_name)
print( * "*")
break
else:
your_name = input("请重新输入用户名:")
your_password = input("请重新输入密码:")
while True:
print( * "*")
print("欢迎来到主菜单,您可进行如下操作!")
print( * "*")
print("| 1 商品列表 | 2 查询购物车 | 3 查看已购商品 | 4 账户余额 | q 退出商城 |")
if menu_num == "":
menu_num = input("请输入:")
#进入商品列表
elif menu_num == "":
while True:
show_goods()
print("| 请输入商品编号加入购物车 | 输入b返回主菜单 | 输入q退出商城 |")
your_goods_num = input("请选择:")
print("\n")
#判断输入的是否为纯数字,如果是
if your_goods_num.isdigit():
while add_gouwuche(your_goods_num,your_name):
#当商品加入购物车后直接返回给购物车
menu_num = ""
break
break
elif your_goods_num == 'b':
menu_num = ""
break
elif your_goods_num == "q":
print("您已退出商城!")
else:
print("您输入的不合法,请重新输入!\n")
#查看购物车
elif menu_num == "":
while True:
print( * "*")
print("您当前购物车商品如下所示:")
show_gouwuche(your_name)
print("| 1 返回主菜单 | 2 删除商品 | 3 支付商品 | q 退出商城 |")
gouwuche_menu_num = input("请输入:")
print("\n")
#返回主菜单
if gouwuche_menu_num == '':
menu_num = ''
break
#删除商品
elif gouwuche_menu_num == '':
while True:
del_gouwuche_num = input("请输入要删除的商品编号,或者输入b返回:")
if del_gouwuche_num == "b":
break
elif not del_gouwuche_num.isdigit():
print("您输入的不合法,请重新输入!\n")
continue
elif del_gouwuche(del_gouwuche_num,your_name):
print("删除成功\n")
break
#支付商品
elif gouwuche_menu_num == "":
while True:
pay_gouwuche_num = input("请输入要支付的商品编号,返回请输入b:")
if pay_gouwuche_num.isdigit():
if pay_gouwuche(pay_gouwuche_num,your_name):
break
elif pay_gouwuche_num == "b":
break
else:
print("您输入的不合法,请重新输入!")
elif gouwuche_menu_num == "q":
print("您已退出商城!")
exit()
else:
print("您输入的不合法,请重新输入\n")
#查看已购买记录
elif menu_num == "":
print( * "*")
print("您已购买商品如下所示:")
show_yigou(your_name)
print("| 输入b返回主菜单 | 输入q退出程序 |")
while True:
yigou_input = input("请输入:")
if yigou_input == "b":
menu_num = ""
print("\n")
break
elif yigou_input == "q":
print("您已退出商城!\n")
else:
print("你输入的不合法,请重新输入!\n")
#查看账户信息
elif menu_num == "":
while True:
print( * "-")
print("您的账户信息如下所示:")
show_money(your_name)
print("| 1 充值账户 | b 返回主菜单 | q 退出商城 |")
money_num == input("请输入:")
if money_num == "":
print( * "-")
add_money_num = input("请输入充值金额,输入b返回:")
if add_money_num.isdigit():
add_money_num = input("请输入充值金额,输入b返回:")
if add_money_num.isdigit():
add_money(add_money,your_name)
print("恭喜你,充值成功!\n")
continue
elif add_money_num == "b":
continue
else:
print("您输入的不合法,请重新输入!\n")
continue
elif money_num == "b":
menu_num = ""
break
elif money_num == "q":
print("您已退出商城!")
exit()
else:
print("您输入的不合法,请重新输入\n")
elif menu_num == "q":
print("您已退出商城!")
exit()
else:
print("您的输入不合法,请重新输入!\n") if __name__ == '__main__':
main()

上面代码是网上找的,准备自己好好写一遍,开始的时候没有什么思路,里面主要是对文件的操作,还有一些新的方法,都是常用的方法,但是平时自己不会用的。

day2、购物商城的更多相关文章

  1. day2 购物商城

    购物商城 商品展示.价格 买,加入购物车 付款,钱不够.(减商品,充值)

  2. day2编写购物商城(1)

    作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够     具体实现了如下功能: 1.可购买的商品信息显示 2.显示购物车内的商品信息.数量.总金额 3.购物车内的商品数量进行增加.减少和商 ...

  3. day2编写购物商城

    作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够 流程图如下: 代码共有4个文件,如下: 用户文件: alex geng zhang lou zeng 商品文件: 小米3 比亚迪宋 格力变 ...

  4. Day2作业:购物商城

    ReadMe: 注意事项: 1.本程序需要提前安装prettytable模块,在商品展示时使用了prettytable 2.数据库使用json模块,有中文数据,在mac系统上编写,运行没有出现问题,在 ...

  5. 微信小程序购物商城系统开发系列-目录结构

    上一篇我们简单介绍了一下微信小程序的IDE(微信小程序购物商城系统开发系列-工具篇),相信大家都已经蠢蠢欲试建立一个自己的小程序,去完成一个独立的商城网站. 先别着急我们一步步来,先尝试下写一个自己的 ...

  6. Python开发程序:ATM+购物商城

    一.程序要求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还 ...

  7. ThinkPHP 购物商城网站(数据库中增删改查的功能实现)——————重点——————

    控制器 ---------------------GoodsController.class.php------------------------------------------------- ...

  8. 微信小程序购物商城系统开发系列

    微信小程序购物商城系统开发系列 微信小程序开放公测以来,一夜之间在各种技术社区中就火起来啦.对于它 估计大家都不陌生了,对于它未来的价值就不再赘述,简单一句话:可以把小程序简单理解为一个新的操作系统. ...

  9. python_day2_homework_1(简单购物商城)

    '''简单购物商城(要求):1,商品展示,价格2,买,加入购物车3,付款,钱不够''' 1 #_*_ coding: utf-8 _*_ __author__ = 'A-rno' meu_list_1 ...

随机推荐

  1. selenium - javascript - 滚动条

    虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法.在这种情况下,就可以借助JavaScript来控制浏览器的滚动条.WebDriver提供了execu ...

  2. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组

    A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集

    A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. UESTC--1300

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1300 分析:dp,最长公共上升子列.对于两个序列num1[maxn],num2[maxn]: 如果num1 ...

  5. 洛谷P1937 [USACO10MAR]仓配置Barn Allocation

    题目描述 Farmer John recently opened up a new barn and is now accepting stall allocation requests from t ...

  6. LuaJavaBridge - lua与java互操作的简单解决方案

    引入:Android平台代码和Lua代码的交互均通过C++和Java交互,Lua再和C++交互(lua  <==> C++ <==> java) 我最开始遇见这种lua调用ja ...

  7. need

    php面试题: http://lib.csdn.net/article/php/43624 小程序: 官方手册: https://mp.weixin.qq.com/debug/wxadoc/dev/d ...

  8. K8S dashboard 创建只读账户

    1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...

  9. 居中div,居中浮动的元素

    定位法:position:absolute 如果子级div有定义宽和高的话就可以用这个方法.注意:margin-top,和margin-left的值均为高和宽值的一半 margin:auto法 这个也 ...

  10. echart图跟随屏幕自适应变化

    var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...