zoukankan      html  css  js  c++  java
  • Python实现购物车小程序

    开发环境,win7、Python3.6、Pycharm社区版2017

    作业需求:

    购物车程序:
    1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表    #再次登陆,输入工资进行充值服务,有没有人性化
    2、允许用户根据商品编号购买商品  
    3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    4、可随时退出,退出时,打印已购买商品和余额
    5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
    6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
    7、允许查询之前的消费记录  #不用查询,下次登录自动打印!苦笑

    先写个不调用文件的“购物车”模拟练习一下:

    这是超市的商品列表

    1 mart_list = [
    2     ("Iphone",5500),
    3     ("Mac Book",9800),
    4     ("Coffee",29),
    5     ("ThinkPad",6690),
    6     ("vcd",100),
    7     ("Channel",2222)
    8 ]

    这是购物车主程序

     1 shopping_car = []
     2 while True:
     3     salary = input("input your salary: ")
     4     if salary.isdigit():
     5         salary = int(salary)
     6         print("当前余额:33[31m%s33[0m"%salary)
     7         while True:
     8             print("product list".center(50,"="))
     9             for k,i in enumerate(mart_list):
    10                 print(k,i)
    11             choice = input("33[36m购物愉快!请选择商品:33[0m")
    12             if choice.isdigit():
    13                 choice = int(choice)
    14                 if choice in range(len(mart_list)):
    15                     buy_product = mart_list[choice]
    16                     if salary < buy_product[-1]:
    17                         print('33[45m余额不足33[0m 当前余额仅剩:33[31m%s33[0m'%salary)
    18                     else:
    19                         print('33[43m%s33[0m 已加入购物车'%buy_product[0])
    20                         shopping_car.append(buy_product)
    21                         salary -= buy_product[-1]
    22                         print('*'*16,' 当前余额:33[31m%s33[0m' % salary)
    23                 else:
    24                     print('33[42m持续更新中,尽情期待……33[0m')
    25             elif choice =="q":
    26                 print('你已购商品列表'.center(42,'-'))
    27                 for i,k in enumerate(shopping_car):
    28                     print(i,k)
    29                 print('本次消费:怎么算,余额:33[31m%s33[0m'%salary)
    30                 exit()
    31             else:
    32                 print('33[42m你的心思,我不太明白!33[0m')
    33     else:
    34         print("33[41m是不是输入的是假币呢?33[0m")
    35         continue

    代码的堆砌,终于完成,总体感觉还能看,

    但是下面的整体:哎,接招吧!烂?我也感觉是

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author: Channel2
    
    mart_list = [
        ("Iphone",5500),
        ("Mac Book",9800),
        ("Coffee",29),
        ("ThinkPad",6690),
        ("VCD",100),
        ("Channel",2222)
    ]
    
    # shopping_car = [("ThinkPad",6690),("ThinkPad",6690),("vcd",100)]
    # with open('userinfo.txt', 'r', encoding='utf-8') as f:
    #
    #     for k in f:
    #         # print(k)
    #         if username in k:
    #             print(k)
    #             username = k.split(':')[0]
    #             password = k.split(":")[1]
    #             salary = k.split(':')[2]
    #             last_shopping = k.split(":")[-1]
    #             print(username,password,salary,last_sho
    
    # while True:
    while True:
        username = input('用户名:')
        with open('lockuser.txt','r',encoding='utf-8') as f:
            for k in f:
                if username not in k:
                    while True:
                        with open('userinfo.txt','r+',encoding='utf-8') as f:
                            for k in f:
                                if username in k:
                                    username=k.split(";")[0]
                                    real_passwd = k.split(';')[1]
                                    salary = int(k.split(';')[-2])
                                    last_shopping = k.split(';')[-1]
                                    # print('')
                                    count = 0
                                    while True:
                                        password = input('请输入密码:')
                                        if password == real_passwd:
                                            print('welcome to my supermarket')
                                            if last_shopping == "0":
                                                pass
                                            else:
                                                print('上次购物是:%s'%last_shopping)
                                            print('你当前余额为:%s'%salary)
                                            # if salary == 0:
                                            while True:
                                                add_money = input('你的工资是:')
                                                if add_money.isdigit():
                                                    add_money = int(add_money)
                                                    salary += add_money
                                                    print('你当前总金额为:33[31m%s33[0m' % salary)
                                                    break
                                                else:
                                                    print('33[41m无法识别你的金额!33[0m 稍稍充值点呗!')
                                            # else:
                                            shopping_car = []
                                            while True:
                                                print("product list".center(50, "="))
                                                for k, i in enumerate(mart_list):
                                                    print(k, i)
                                                choice = input("33[33m购物愉快!请选择商品:33[0m")
                                                if choice.isdigit():
                                                    choice = int(choice)
                                                    if choice in range(len(mart_list)):
                                                        buy_product = mart_list[choice]
                                                        if salary < buy_product[-1]:
                                                            print('33[43m余额不足33[0m 当前余额仅剩:33[31m%s33[0m' % salary)
                                                        else:
                                                            print('33[45m%s33[0m  已加入购物车' % buy_product[0])
                                                            # with open('userinfo','r+',encoding='utf-8') as f:
    
                                                            shopping_car.append(buy_product)
                                                            salary -= buy_product[-1]
                                                            print('当前余额:33[31m%s33[0m'%salary)
                                                    else:
                                                        print('33[46m正在更新中,尽情期待33[0m')
                                                elif choice == "q":
                                                    print('你已购商品列表'.center(40,'-'))
                                                    for i, k in enumerate(shopping_car):
                                                        print(i, k)
                                                    print('本次消费后,余额33[31m%s33[0m' % salary)
                                                    with open('userinfo.txt','r+',encoding='utf-8') as f:
                                                        f.read()
                                                        # print(f.tell())
                                                        f.write(';')
                                                        f.write(str(salary))
                                                        # f.write(';',str(salary))
                                                        f.flush()
                                                        f.write(';')
                                                        f.write(str(shopping_car))
                                                        f.flush()
                                                    exit()
                                                else:
                                                    print('33[32m你的心思,我很难猜!33[0m')
                                        else:
                                            if count == 2:
                                                print("33[42m账号错误太多,将被锁定,请联系管理员!33[0m")
                                                with open('lockuser.txt','w',encoding='utf-8') as f:
                                                    f.write(username)
                                                    exit()
                                            else:
                                                print('33[43m密码错误!33[0m')
                                                count += 1
                                else:
                                    print('用户不存在')
                                    exit()
                else:
                    print('此账号被锁定,请手动删除lock.user.txt的内容,重新再试!')
                    exit()
    

    好歹也辛苦构思三天了,要不我给你指导一下?

    首先文件结构:主程序就不用说了,辅助两个文件lockuser.txt当密码输入错误超过三次,则锁定。默认不可为空。

        userinfo.txt保存用户的用户名;密码;余额;上次购买物品。下次再购买,则继续追加“;余额” 和 “;本次购买的商品”

      1.运行主程序,输入用户名,和密码

      2.读取账号余额,提示输入工资,不输入就不让继续了,嘿嘿嘿!耍流氓了!

      3.恩恩,购物开始,输入对应的商品编号,否则,看提示!

      4.购物途中可以随时退出,退出时,打印你的购物清单,和账号余额

        同时把你的余额,和购物记录,追加至userinfo.txt 文件中

      5.第二次登陆,仍然需要输入工资的,多少输入一点吧,

      6.接5,打印出你上次的购买清单,样子有点丑,呵呵

    总结:

      这次作业用到的知识有,while 循环、for循环、if判断、enumerate()、文件的读取和写入(列表到字符串的转换)(字符串转化为列表???)

      装B新技能 打印彩色字体,恩linux里面也有格式(33[31m加色字33[0m

    好了,纠结了我两天的程序就先这样吧,我的态度很不好吗?哎,书到用时方恨少啊。下次见!

  • 相关阅读:
    Django视图层进阶、模板层
    【python学习笔记:Django】3.生活需要仪式感——Hello World
    【python学习笔记:Django】2.启动虚拟环境库出错——Windows PowerShell中无法加载文件 xxxScriptsActivate.ps1,因为在此系统上禁止运行脚本
    【python学习笔记:Django】1.开发环境搭建——“三剑客”:python、django、visual studio code
    【window使用技巧】如何提高某盘下载速度?
    【Python应用】爬取LOL皮肤图片(面向过程编程)
    【Python图形界面编程】:PyQt5编程入门(看这篇就够了!)
    android调用百度地图(1)新手环境搭建基础应用教程
    android实现调用科大讯飞语音识别功能详细步骤
    Genymotion模拟器下载安装到连接使用一站式教程
  • 原文地址:https://www.cnblogs.com/yes5144/p/6845411.html
Copyright © 2011-2022 走看看