实现:
判断用户身份
(1)商家
商品信息存在文件“商品列表”里,可以增加商品和修改商品价格
(2)顾客
1.打印商品列表(与商家所用列表同步),从文件中读取余额
2.允许用户根据商品名称购买商品
3.用户选择商品后,监测余额是否够,够就直接扣款,不够就提醒
4.已购商品与余额保存至“购物清单”文件
# -*- coding: utf-8 -*-
"""
@Author:yhf1995
@time:2020/2/2 20:00
"""
info={'1':'顾客', '2': '商家'}
for i in info:
print(i,info[i])
choice=input("请选择您的身份>>")
product_list = [] # 存储商品名称和价格的列表
if choice == '1': # 顾客入口
with open("product_list", "r", encoding="utf-8") as f:
# 读商品列表的文件,存储格式是Iphone,8500,换行,然后下一个
for line in f:
(name, value) = line.strip().split(",") # 按照“,”分片存储为一个元组
product_list.append([name, int(value)]) # 添加到商品列表中
# 读取现在拥有的金额
with open("salary", "r", encoding="utf-8") as f_salary:
for line in f_salary:
salary = line
shopping_list = [] # 存购买商品的列表
if salary.isdigit():
salary = int(salary)
# 判断输入的金额是否为数字,如果是将其转为int类型,如果不是,输出 输入有误
while True:
for item in product_list:
print(product_list.index(item), item)
# 打印商品列表
choice_number = input("Please enter the number of the product you want:>>>")
if choice_number.isdigit():
choice_number = int(choice_number)
# 判断输入的商品编号是否为数字,如果是转为int型,如果不是,提示输入有误
if 0 <= choice_number < len(product_list):
# 判断商品是否存在
if salary >= product_list[choice_number][1]:
shopping_list.append(product_list[choice_number])
salary -= product_list[choice_number][1]
print("add shop %s in your shopping cart,and your salary is \033[31;1m%s\033[0m!"%(product_list[choice_number],salary))
# 判断余额是否足够,余额足够,将商品放入购物车,余额减去该商品价格,输出放入购物车的商品和余额
else:
print("You have insufficient balance!")
# 余额不足
else:
print("product_number does not exist!")
# 商品不存在
elif choice_number == "Q":
print("The shopping cart list is as follows:")
# 打印购买商品列表
for shop_item in shopping_list:
print(shop_item)
# 打印剩余金额
print("Your current balance is %s." % ( salary ))
# 更新以购买商品文件
with open("shopping_list","a",encoding="utf-8") as f_shop_cart:
f_shop_cart.write("\n")
for shop_item in shopping_list:
f_shop_cart.write(shop_item[0])
f_shop_cart.write('\t')
f_shop_cart.write(str(shop_item[1]))
f_shop_cart.write("\n")
# 更新以剩余金额文件
with open("salary","w",encoding="utf-8") as f_salary:
f_salary.write(str(salary))
exit()
# 输入Q 打印购物车商品和余额,并退出购物车程序
else:
print("The product_number is error!")
# 商品编号有误
else:
print("The amount entered is not a number!")
# 金额有误
elif choice == '2':
with open("product_list", "r", encoding="utf-8") as f:
for line in f:
(name, value) = line.strip().split(",")
product_list.append([name, int(value)])
for item in product_list:
print(product_list.index(item), item)
# 打印商品列表
choice2 = input("请输入操作代号,U表示修改商品价格,A表示增加商品:")
if choice2 == "U":
while True:
choice_update_number = input("Please enter the number of the product you want update:>>>")
if choice_update_number.isdigit():
choice_update_number = int(choice_update_number)
# 判断输入的商品编号是否为数字,如果是转为int型,如果不是,提示输入有误
if 0 <= choice_update_number < len(product_list):
# 判断商品是否存在
update_value = input("请输入修改后的商品价格:")
if update_value.isdigit():
product_list[choice_update_number][1] = update_value
else:
print("输入的价格不是数字!")
else:
print("product_number does not exist!")
# 商品不存在
elif choice_update_number == "Q":
with open("product_list", "w", encoding="utf-8") as f:
for shop_item in product_list:
f.write(shop_item[0])
f.write(',')
f.write(str(shop_item[1]))
f.write("\n")
exit()
# 输入Q 打印购物车商品和余额,并退出购物车程序
else:
print("输入的内容有误,请输入商品序号,退出请输入Q!")
elif choice2 == "A":
while True:
shop_name = input("请输入要增加的商品名称,或者输入Q退出:")
if shop_name != "Q":
shop_value = input("请输入要增加的商品价格:")
product_list.append([shop_name,shop_value])
else:
with open("product_list", "w", encoding="utf-8") as f:
for shop_item in product_list:
f.write(shop_item[0])
f.write(',')
f.write(str(shop_item[1]))
f.write("\n")
exit()
else:
print("您输入的操作代号有误!")
# 商家需要登录#
else:
print("您输入的身份代码有误!")
所需的文件有product_list,shoping_list 和salary
Product_list里的内容格式如下:
Iphone,8500
Mac Pro,11200
Starebuck Latte,31
Alex python,81
Bike,800
motobike,5000
shoping_list里面最初是空的
salary里面 就只有一个数字
本程序实现了基本功能,本来还想在商家的那部分加入上一个作业的登陆接口,登陆成功以后才能进行商家操作,但发现都是一个文件读取的套路,就没加,主要还是懒,哈哈哈
题目来源:oldboy教育python课程