购物车-上-用户程序

购物车-上-用户程序:
一、 程序要求:
1、 商品列表存在文件里,用户进入程序后,输出商品列表
2、 每次用户买完东西后,将购买记录存入一个日志文件
3、 可随时退出,退出后打印购买记录及余额

二、 程序流程图

在这里插入图片描述

三、 Readme
1、 输入用户名,工资,用户名用于购买记录及退出后的打印输出
2、 输入相应商品序号即表示购买,扣款并打印输出商品信息,增加商品名及价格到一个临时列表
3、 程序中输入不合法时将提示重新输入
4、 购买完成后,程序会将临时列表中的所有商品写入日志文件

四、 程序代码及注释

#!/usr/bin/env python
#Author:lisheng

user_car = []   #待写入的购买日志临时列表
car = []   #商品输出列表
jugdement = False   #循环执行与否的决定变量

product1 = open("product.txt", "r")   #读出商品列表,并赋给car列表
for i1 in product1.readlines():
    i1 = i1.strip()
    pro = i1.split(" ")[0]
    pri = i1.split(" ")[1]
    car.append(["{ss}".format(ss=pro), "{ssd}".format(ssd=pri)])
product1.close()

name = input("your name:")
while not jugdement:
    salary = input("your salary:")
    if salary.isdigit():   #工资输入合法后,才会继续
        salary = int(salary)   #将输入的工资格式化为数字
        while not jugdement:   #打印商品列表,并自动添加序号
            for i_r in enumerate(car):
                print(i_r)
            chooes_user = input(">>>(input q tu exit!):")   #输入序号,选择商品
            if chooes_user.isdigit():   #序号输入合法后,才会继续
                chooes_user = int(chooes_user)   #将输入的序号格式化为数字
                if chooes_user < len(car) and chooes_user >= 0:   #判定序号是否在列表中
                    if salary >= int(car[chooes_user][1]):   #工资大于商品,从工资中减去商品价格,将所选商品写入列表,同剩余工资一并打印在屏幕上
                        salary -= int(car[chooes_user][1])
                        print("you put %s in you shopping car!" % (car[chooes_user][0]))
                        user_car.append(["{a1}".format(a1=car[chooes_user][0]), "{a2}".format(a2=car[chooes_user][1])])
                        print("your salary is :", salary)
                    elif salary < int(car[chooes_user][1]):   #给出工资不足的提示,并要求重新输入序号
                        print("you do not have enough money!")
                        print("your salary is :", salary)
                else:   #输入除数字以外的字母或符号时,提示输入正确号码
                    print("please input the right number!")
            elif chooes_user == "q":
                jugdement = True
            else:
                print("input the right number!")

print("{name}`s shopping_list".format(name = name).center(100,"-"))   #打印购物列表,并存入日志文档
user_car_list = open("user_car_list.txt","a+")
user_car_list.write("{n}\n".format(n = name))
for record in user_car:
    user_car_list.write("{r},".format(r = record))
    print(record)
user_car_list.write("\n>>>\n")
user_car_list.close()
print("you salary is :",salary)
在这里插入代码片

五、 附件
1、 商品文件格式
iphone 6000
ak47 5000
tv 3500
three_star 100
pan 200
ss 500
tv4500 4500
tt 20
wuzi 4000

2、 购物日志文件格式
ls
[‘iphone’, ‘5000’],[‘ak47’, ‘3500’],[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],

ss
[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],[‘pan’, ‘300’],

ff
[‘three_star’, ‘100’],[‘ak47’, ‘4000’],[‘ss’, ‘500’],

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值