python学习之登陆购物
admin
2023-07-20 03:21:55
0

#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 + '\t')
user_shop_file.write(str(user_salary) + '\n')
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 + '\t')
user_shop_file.write(str(user_salary) + '\n')
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 + '\t')
user_shop_file.write(str(user_salary) + '\n')
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 + '\t')
user_shop_file.write(str(user_salary) + '\n')
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()

相关内容

热门资讯

聚力奋进新型工业化之路 □本报记者 孙铭阳 机器轰鸣的生产车间里,数字化生产线高效运转;科创园区实验室中,前沿技术持续突破;...
三星确认Exynos 2700... IT之家 6 月 18 日消息,据《韩国经济日报》报道,三星电子半导体 LSI 事业部总裁 Park...
伊朗最高领袖:美国总统出于绝望... 当地时间6月18日晚,伊朗最高领袖穆杰塔巴·哈梅内伊就伊美签署谅解备忘录发表声明。穆杰塔巴表示,“正...
互相扯皮,虫害再成美两党政治博... 美国农业部16日发布声明称,将投入约1.05亿美元用于新项目,强化美国对螺旋锥蝇的防控能力。本月以来...
高市插不上话只能尬笑,日本发G... 根本插不上话,只能尬笑!6月17日,日本方面发布了一张高市早苗在G7峰会的照片,配文“高市与附近领导...
侯宏、李智勇 | 智能原生组织... 来源:北大国发院 导语 AI为组织这艘航船提供了无限的动力、实时的全球海图、自动驾驶系统与自我强化...
民企奋进自贸港:“海南是技术落... 中新网海口6月18日电 题:民企奋进自贸港:“海南是技术落地的重要实践场” 中新网记者 符宇群 “海...
美国白宫向国会提交美伊谅解备忘... 央视记者当地时间6月18日获悉,美国白宫向国会提交了美伊谅解备忘录文本。当地时间17日,一名美国政府...
原创 第... > 姜辰的档案有一行特殊的备注。 > “此人在SpaceX的核心研发团队中担任轨道力学组长。她的入职...
“即便美国赢下对华AI竞争,也... 【文/观察者网 柳白】 美国正全力在AI领域与中国竞争,密集布局相关战略,但有美国学者担忧:过度聚焦...