python学习之登陆购物
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习之登陆购物相关的知识,希望对你有一定的参考价值。
#author:yangxiao
‘‘‘
购物车需求
用户入口:
1.商品信息存在文件里
2.已购商品,余额记录。第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额
3.允许用户根据商品编号购买商品
4.用户选择商品后,检测是否够,够就直接扣款,不够就提醒
5.可随时退出,退出时,打印移购买商品和余额
商家入口:
1.可以添加商品,修改商品价格
‘‘‘
import os,sys
def user_shopping():
while True:
user_salary = input(‘请输入你的充值金额:‘)
if user_salary.isdigit():
user_salary = int(user_salary)
user_shop_file.write(login_name + ‘ ‘)
user_shop_file.write(str(user_salary) + ‘
‘)
for item in product_lists:
print(product_lists.index(item),item)
p_index = product_lists.index(item)
while True:
user_choice = input(‘请输入你要选择的商品:‘)
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice <= p_index or user_choice >= 0:
p_item = product_lists[user_choice]
p_item[1] = int(p_item[1])
if user_salary >= p_item[1]:
user_salary -= p_item[1]
global shop_lists
shop_lists.append(p_item)
user_shop_file.seek(0)
user_shop_file.write(login_name + ‘ ‘)
user_shop_file.write(str(user_salary) + ‘
‘)
print(‘商品%s已加入购物车,您还剩%s钱‘%(p_item[0],user_salary))
else:
print(‘您的金额不错,请去充值‘)
print(‘您已购得‘.center(30,‘*‘))
print(shop_lists)
sys.exit()
else:
print(‘请输入范围内的商品:‘)
elif user_choice == ‘q‘:
sys.exit(‘退出程序‘)
else:(‘其输入正确的商品编号‘)
else:
print(‘请输入正确的格式‘)
def user_top():
while True:
user_salary = input(‘请输入你要充值的金额:‘)
if user_salary.isdigit():
user_salary = int(user_salary)
user_salary += int(shop[1])
user_shop_file.seek(0)
user_shop_file.write(login_name + ‘ ‘)
user_shop_file.write(str(user_salary) + ‘
‘)
for item in product_lists:
print(product_lists.index(item),item)
p_index = product_lists.index(item)
while True:
user_choice = input(‘请输入你要购买的商品:‘)
user_choice = int(user_choice)
if user_choice <= p_index or user_choice >= p_index:
p_item = product_lists[user_choice]
p_item[1] = int(p_item[1])
if p_item[1] <= user_salary:
user_salary -= p_item[1]
global shop_lists
shop_lists.append(p_item)
user_shop_file.seek(0)
user_shop_file.write(login_name + ‘ ‘)
user_shop_file.write(str(user_salary) + ‘
‘)
print(‘商品%s已加入购物车,您还剩%s钱‘%(p_item[0],user_salary))
else:
print(‘您的金额不错,请去充值‘)
print(‘您已购得‘.center(30,‘*‘))
print(shop_lists)
sys.exit()
else:
print(‘请输入正确的商品范围‘)
else:
print(‘请输入争取的格式的格式‘)
user_name = ‘‘
shop_lists = []
product_lists = []
def product_shop():
user_product_list_file = open(‘product_file.txt‘,‘r+‘,encoding =‘UTF-8‘)
product_list_item = user_product_list_file.readlines()
for i in product_list_item:
i = eval(i)
product_lists.append(i)
product_shop()
print(product_lists)
user_shop_file =open(‘user_shop_file.txt‘,‘r+‘)
user_shop_list =user_shop_file.readlines()
for i in user_shop_list:
shop = i.split()
print(i)
if len(user_shop_list) == 0:
login_name = input(‘请输入用户名:‘)
user_info_file = open(‘user_info_file.txt‘,‘r+‘)
user_info_list = user_info_file.readlines()
for a in user_info_list:
a = a.split()
if login_name in a:
while True:
login_pwd = input(‘请输入密码‘)
if login_pwd == a[1]:
print(‘欢迎进入购物商城‘)
user_shopping()
else:
print(‘你输入的密码不争取‘)
else:
sys.exit(‘用户名不存在‘)
else:
login_name = input(‘请输入用户名:‘)
user_info_file = open(‘user_info_file.txt‘,‘r+‘)
user_info_list = user_info_file.readlines()
for a in user_info_list:
a = a.split()
if login_name in a:
while True:
login_pwd = input(‘请输入密码‘)
if login_pwd == a[1]:
print(‘欢迎进入购物商城‘)
user_top()
以上是关于python学习之登陆购物的主要内容,如果未能解决你的问题,请参考以下文章