python购物程序_python3 购物程序

要求:

一、启动程序后,选择是商家还是用户

1、选择商家用户

输入用户名,密码进入

选择增加商品及价格:格式: 商品名称 价格

选择编辑商品及价格:根据提示进行操作

2、选择用户

输入用户名,密码进入

判断是否是第一次登录,如果是,提示进行充值

充值完成,显示商品列表,输入商品名称进行购买,检测余额是否够,够就直接扣款,不够提示余额不足。

3、可随时退出,退出时,打印已购买商品和余额。

python 版本 3.5

主函数:

#Author by Andy

#_*_ coding:utf-8 _*_

import sys,time,os

sys.path.append("E:\my python study\day2")

import login_new

#定义显示商品列表函数

def product_list():

with open(r'E:\my python study\files\product_list.txt',encoding='utf-8') as f:

for index,item in enumerate(f.readlines()):

print(item,end='')

print('\n')

#定义增加商品函数

def add_product():

while True:

with open(r'E:\my python study\files\product_list.txt', 'a+', encoding='utf-8') as f:

Product=input('请输入增加的商品名称及价格:')

if Product=='q' or Product=='Q':

product_list()

exit()

else:

f.write(Product)

f.write('\n')

f.close()

product_list()

#定义修改商品价格函数

def edit_product():

while True:

f2 = open(r'E:\my python study\files\product_list.txt', 'r+', encoding='utf-8')

with open(r'E:\my python study\files\product_list.txt','r+',encoding='utf-8') as f:

print("您的商品列表:")

product_list()

old=input("请输入要修改的商品名称及价格:")

if old == 'q' or old == 'Q':

print(product_list())

exit()

else:

new=input("请输入修改后的商品名称及价格:")

if new == 'q' or new== 'Q':

print(product_list())

exit()

else:

for line in f:

if old in line:

line=line.replace(old,new)

f2.write(line)

print(product_list())

#定义判断是否第一次登录及充值函数

def chk_first_login():

if os.path.exists(r"E:\my python study\files\balance.txt"):

with open(r'E:\my python study\files\balance.txt', 'r', encoding='utf-8') as f:

for Balance in f.readlines():

print('欢迎光临,您目前的余额为:', Balance)

else:

with open(r'E:\my python study\files\balance.txt', 'w+', encoding='utf-8') as f:

print('欢迎光临,第一次使用,请先充值!')

Balance = input('请输入充值金额:')

f.write(Balance)

print('正在充值,请稍候', end='')

for i in range(10):

sys.stdout.write('.')

sys.stdout.flush()

time.sleep(1)

print('\n充值成功!祝您购物愉快^_^')

#定义获取价格函数

def get_price(x):

with open(r'E:\my python study\files\product_list.txt',encoding='utf-8') as f:

item=[]

price=[]

for i in f.readlines():

item.append(i.split()[0])

price.append(i.split()[1])

s=zip(item,price)

d={}

for k,v in s:

d[k]=v

if x not in d.keys():

print("输入错误,请重新输入!")

buy_main()

else:

return int(d[x])

#定义获取余额函数

def get_balance():

with open(r'E:\my python study\files\balance.txt', 'r', encoding='utf-8') as f:

balance=f.read()

return int(balance)

#定义购物车记录函数

def shopping_record(record):

with open(r'E:\my python study\files\shopping_cart.txt', 'a+', encoding='utf-8') as f:

for i in record:

f.write('%s\n'%(i))

#定义获取购物记录函数

def get_shopping_record():

with open(r'E:\my python study\files\shopping_cart.txt', 'r', encoding='utf-8') as f:

print(f.read())

#定义购买函数

def buy_main():

choice=input("输入商品名称购买商品,退出请按q!:")

if choice == 'q' or choice =='Q':

print('我的购物车:',end='')

get_shopping_record()

print("欢迎下次光临,再见!")

exit()

else:

Price = int(get_price(choice))

print("您要购买的商品价格为:%s" %(Price))

Balance=get_balance()

if Price > Balance:

print("\033[31;1m对不起,您的余额不足!\033[0m")

else:

print("您购买的%s 已添加至购物车"%(choice))

Balance=str(Balance - Price)

with open(r'E:\my python study\files\balance.txt', 'w+', encoding='utf-8') as f:

f.write(Balance)

Shopping_cart=[]

Shopping_cart.append(choice)

shopping_record(Shopping_cart)

#######################################################################################################################

def fun_customers():

login_name = input("Login:")

login_pass = input("Password:")

login_new.confirm(login_name,login_pass)

chk_first_login()

time.sleep(1)

print("我们有一下商品供您选购:")

product_list()

while True:

buy_main()

def fun_seller():

login_name = input("Login:")

login_pass = input("Password:")

login_new.confirm(login_name, login_pass)

print("您的商品列表:")

product_list()

while True:

print("增加商品输入a,修改商品价格输入)c:")

choice=input(":")

if choice=='a':

add_product()

elif choice=='c':

edit_product()

else:

print("输入错误,请重新输入!")

#####################################################################################################################

print("商家入口输入s")

print("用户入口输入c")

role=input(":")

while True:

if role =='c' or role=='C':

fun_customers()

elif role == 's' or role == 'B':

fun_seller()

elif role == 'q' or role == 'Q':

print("谢谢使用!")

else:

print('输入错误,请重新输入!')

用户名密码判断函数:

#Author by Andy

#_*_ coding:utf-8 _*_

#定义校验用户名及密码函数

def confirm(x,y):

login_name = x

login_pass = y

_username = open(r"E:\my python study\files\username.txt")

username_list = []

for username in _username.readlines():

username_list.append(username.strip("\n"))

_username.close()

_password = open(r"E:\my python study\files\password.txt")

password_list = []

for passwd in _password.readlines():

password_list.append(passwd.strip("\n"))

_password.close()

passwd_dict = {}

s = zip(username_list, password_list)

for k, v in s:

passwd_dict[k] = v

if passwd_dict[login_name] != login_pass:

print ("username or password wrong!")

return 1

else:

# print ("Welcome!")

return 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Python 字典实现简单购物车的示例代码: ```python # 商品列表,每个商品包括名称和价格 goods = { 'apple': 3, 'banana': 2, 'orange': 4, 'watermelon': 5, 'pear': 3.5 } # 购物车,每个商品名称对应数量 cart = {} while True: # 打印商品列表和购物车 print('商品列表:') for name, price in goods.items(): print(f'{name}: {price}') print('当前购物车:') for name, count in cart.items(): print(f'{name}: {count}') # 获取用户输入 cmd = input('请输入操作指令(结账/添加/删除):') if cmd == '结账': # 计算总价并输出 total_price = sum(goods[name] * count for name, count in cart.items()) print(f'您需要支付 {total_price} 元') break elif cmd == '添加': # 获取要添加商品名称和数量 name = input('请输入要添加商品名称:') count = int(input('请输入要添加商品数量:')) # 更新购物车 if name in cart: cart[name] += count else: cart[name] = count elif cmd == '删除': # 获取要删除的商品名称和数量 name = input('请输入要删除的商品名称:') count = int(input('请输入要删除的商品数量:')) # 更新购物车 if name in cart: cart[name] -= count if cart[name] <= 0: del cart[name] else: print('购物车中没有该商品') else: print('无效的操作指令') ``` 运行以上代码程序会进入一个循环,每次循环打印商品列表和购物车,并等待用户输入操作指令。用户可以输入“结账”结束购物并计算总价,输入“添加添加商品购物车,输入“删除”从购物车中删除商品程序会根据用户输入更新购物车,并不断循环直到用户输入“结账”为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值