python3 简单的购物程序

要求:
1.启动程序后提示用户登录,登录错误3次退出程序
2.登录成功打印商品列表
3.允许用户根据商品编号购买物品,
4.商品列表是从文件读取,剩余数量实时更新
4.用户选择商品后 检查余额是否足够 够直接扣款 不够提醒

import time
import sys


'''欢迎界面函数'''
def welcome(user_name,balance,now_balance,chongzhi,shop_num):
    print('''\033[31;1m
=================================================================================
=                        欢迎来到fockrock的购物平台                                 =
=================================================================================
帐户:%s\t帐户金额:%d\t当前余额%d\t充值金额%d\t购物车数量%d\033[0m'''%(user_name, balance, now_balance, chongzhi, shop_num))

'''商品展示函数'''
def list_goods(goods_list):

    print('''\033[34;1m
商品列表:
=================================================================================
%-5s%-10s%-10s%-10s%-10s'''%('编号', '商品名称', '商品价格(元)', '商品总数量', '商品剩余数量'))
    for good_list in goods_list:
        (bianhaos, good_name, price, good_num, shengyu_num) = good_list.strip('\n').split()
        print('%-5s%-15s%-15s%-10s%-10s'%(bianhaos, good_name, price, good_num, shengyu_num))


i = 0
while i < 3:    # 帐户密码错误3次退出循环
    print('\033[32;1m=================================================================================')
    user_name = input('请输入您的用户名:')
    user_psw = input('请输入密码:\033[0m')
    users_file = open('users_list.txt', 'r')
    users_list = users_file.readlines()
    users_file.close()
    for user_line in users_list:
        (user, password, balance) = user_line.strip('\n').split(' ')
        # print(user, password, balance)
        balance = int(balance)
        now_balance = balance
        chongzhi = 0
        shop_num = 0
        shopping_car_list = {}        # 初始化购物车
        if user_name == user and user_psw == password:

            exit_flag1 = 1
            while exit_flag1:
                welcome(user_name, balance, now_balance, chongzhi, shop_num)   # 打印欢迎界面
                goods_file = open('goods_list.txt', 'r')
                goods_list = goods_file.readlines()
                goods_file.close()  # 关闭商品列表
                list_goods(goods_list)   #打印商品列表
                choice = input('请选择菜单 :输入商品编号 | 购物车(c) | 余额充值(r) | 结帐(j) | 退出(q) : ')   # 打印用户选择项
                if str.isdigit(choice):
                    choice = int(choice)
                if choice == 'q':  # 退出程序
                    sys.exit()
                elif choice == 'c':   # 进入购物车
                    pass
                elif choice == 'r':    # 进入充值流程
                    pass
                elif choice == 'j':     # 进入结账流程
                    pass
                elif type(choice) is int and choice > 0 and choice <= (len(goods_list)+1):  # 用户输入数字且大于0小于商品列表数,进入
                    # print(goods_list)
                    choice_good = goods_list[choice-1].strip('\n').split(' ')  # 选择的商品转换为列表
                    # bianhaos, good_name, price, good_num, shengyu_num
                    (bianhaos, good_name, price, good_num ) = (int(choice_good[0]), choice_good[1], int(choice_good[2]), int(choice_good[3]))
                    shengyu_num = int(choice_good[4])

                    # print(choice_good)
                    print('\033[35;1m您选择的商品是{},价格为{}元,剩余{}个\033[0m'.format(good_name, price, shengyu_num))   # 输出用户选择的商品信息

                    while True:   # 进入商品购买循环
                        numbers = input('\033[35;1m请输入想要购买的数量:(最大{})| 返回(b)| 退出(q)\033[0m'.format(shengyu_num))  # 输入购买的数量
                        if str.isdigit(numbers):   # 输入数字转换为int
                            numbers = int(numbers)
                        if numbers == 'b':   # b退出当前循环
                            break
                        elif numbers == 'q':  # q退出程序
                            sys.exit()
                        elif type(numbers) is int and int(numbers) > 0 and int(numbers) < int(choice_good[3]):
                            good_sum = int(numbers)*int(choice_good[2])
                            if now_balance >= good_sum:
                                now_balance -= good_sum
                                shengyu_num -= numbers
                                goods_list[choice-1] = '{} {} {} {} {}\n'.format(bianhaos, good_name, price, good_num, shengyu_num)
                                # print(shengyu_num)
                                print('\033[35;1m购买的商品为{},当前余额{}元\033[0m'.format(good_name, now_balance))

                                '''回写商品列表'''
                                goods_file2 = open('goods_list.txt', 'w', encoding='utf-8')
                                for i in goods_list:
                                    goods_file2.write(i)
                                goods_file2.close()
                            else:
                                print('\033[36;1m余额不足,请重新选择\033[0m')
                                time.sleep(3)  # 停3秒返回商品列表
                                break




    else:
        # print('帐户密码错误,请重新输入')
        i += 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值