温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python学习之登陆购物

发布时间:2020-07-25 12:37:23 来源:网络 阅读:734 作者:皮皮球 栏目:编程语言

#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()

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI