#!/usr/bin/env python #_*_ coding:utf-8 _*_#Author:LiuJindong #datetime:2018/6/11 16:34 ‘‘‘此程序为购物车程序,包括用户入口、商家入口.用户入口:已购商品、余额记录.商家入口:可以添加商品、修改商品价格.‘‘‘ import time,sys def Business_entrance(ProductList): f1=open(ProductList,‘r‘) Product={} for line in f1: line=line.strip() if len(line)>0: k,v=line.split() Product[k]=v f1.close() print(‘\033[33;1m商品 价格\033[0m‘.center(50,‘*‘)) for k,v in Product.items(): print(‘\033[32;1m%s %s\033[0m‘%(k,v)) Flag=True while Flag: User_Choice=input(‘请选择添加商品(A|a)|修改商品价格(C|c)|退出程序(Q|q):‘).strip() if User_Choice==‘A‘ or User_Choice==‘a‘: while True: with open(ProductList,‘a‘) as f2: Pro_Name1=input(‘请填写需要添加的商品名称:‘).strip() if Pro_Name1 not in Product.keys(): Pro_Price1 = input(‘请填写商品价格:‘).strip() Res=Pro_Name1+‘ ‘+Pro_Price1+‘\n‘ f2.write(Res) print(‘%s,已添加到商品列表中‘%Pro_Name1) break else: print(‘商品已存在,请重新输入.‘) elif User_Choice==‘C‘ or User_Choice==‘c‘: while True: Pro_Name2 = input(‘请填写修改的商品名称:‘).strip() if Pro_Name2 in Product.keys(): Pro_Price2=input(‘请填写修改后的商品价格:‘).strip() with open(ProductList, ‘r‘) as f3: for line in f3: line=line.strip().split() if Pro_Name2 in line: Product[Pro_Name2] = Pro_Price2 print(‘%s价格修改成功‘ % Pro_Name2) f4=open(ProductList,‘w‘) for k, v in Product.items(): Res=k+‘ ‘+v+‘\n‘ f4.write(Res) else: print(‘准备返回上一级菜单‘) time.sleep(2) break else: print(‘您填写的商品不存在,请重新填写.‘) continue elif User_Choice==‘Q‘ or User_Choice==‘q‘: print(‘准备退出程序‘) time.sleep(2) Flag=False else: print(‘您输入有误,请重新输入.‘) continue return ‘商家入口程序已成功运行‘ def Person_entrance(Productlist,Shopping_Cart,Balance_Record): Product={} with open(Productlist,‘r‘) as f1: for line in f1: line=line.strip() if len(line)>0: k,v=line.split() Product[k]=v print(‘\033[33;1m商品名称 价格\033[0m‘.center(50,‘*‘)) for k,v in Product.items(): print(‘\033[34;1m%s %s\033[0m‘%(k,v)) Flag=True while Flag: Salary=input(‘请输入您的工资:‘).strip() if Salary.isdigit(): Salary=int(Salary) while True: Pro_Name=input(‘输入购买商品名称|退出程序(Q|q):‘).strip() if Pro_Name in Product.keys(): f2=open(Shopping_Cart,‘a‘) f3=open(Balance_Record,‘w‘) if Salary>=int(Product[Pro_Name]): print(‘%s,购买成功.‘%Pro_Name) Salary-=int(Product[Pro_Name]) print(‘您的余额为:%d‘%Salary) Shopping_list=Pro_Name+‘ ‘+Product[Pro_Name]+‘\n‘ f2.write(Shopping_list) f3.write(str(Salary)) elif Salary<=0: print(‘您的余额不足,欢迎下次再次光临.‘) f3.write(str(Salary)) time.sleep(2) sys.exit() else: print(‘您的余额不足以购买此商品,请重新选择.‘) continue elif Pro_Name==‘Q‘ or Pro_Name==‘q‘: print(‘准备退出程序‘) time.sleep(2) sys.exit() else: print(‘您输入的商品名称不存在,请重新输入.‘) continue else: print(‘您输入的工资格式有误,请重新输入.‘) continue if __name__==‘__main__‘: Business_entrance(‘Product_list‘) Person_entrance(‘Product_list‘,‘Shopping_Cart.txt‘,‘Balance_Record.txt‘)
原文地址:https://www.cnblogs.com/moshell/p/9172396.html
时间: 2024-10-11 02:50:50