# -*- coding: utf-8 -*-
"""
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃ 永无BUG!   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""
#功能选择1.注册2.登入3.购物车4.充值5.支付
#商品列表需要是以下形式的列表['序号:商品名称:价格']
#许先创建一个login.txt文件
#可以往shop_list列表中添加商品
shop_list=['1:电脑:5000','2:手机:3000','3:衣服:1000','4:鞋子:500','5:零食:100','6:玩具:50','7:水果:50']
import os
def login():
    '''用户名和密码的注册'''
    username=input('请输入用户名>>>:').strip()
    userpwd=input('请输入密码>>>:').strip()
    with open(r'login.txt','a+t',encoding='utf-8')as f:
        for line in f:
            res=line.strip('\n').split(':')
            if res[0]==username:
                print('用户名已注册过')
                break
        else:
            f.write('%s:%s\n'%(username,userpwd))
            print('注册成功')
    f.close()
    return username
def enter():
    '''用户的登入认证'''
    username = input('请输入用户名>>>:').strip()
    userpwd = input('请输入密码>>>:').strip()
    with open(r'login.txt', 'r+', encoding='utf-8')as f:
        for line in f:
            res=line.strip('\n').split(':')
            if res[0]==username and res[1]==userpwd:
                print('登入成功')
                break
        else:
            print('用户名还没注册')
    f.close()
    return username

def shop():
    '''选择商品和数量,加入新列表中'''
    commodity_list=[]
    while True:
        print('0:退出')
        for commodity in shop_list:
            print(commodity)
        input_number = input('请选择商品号码>>>:').strip()
        if int(input_number) not in range(0, len(shop_list)+1):
            print('选择错误')
        ':
            break
        else:
            shop_count = input('请选择商品数量>>>:').strip()
            print()
            for commodity in shop_list:
                commodity_number,commodity_name,commodity_price=commodity.split(':')
                if input_number==commodity_number:
                    commodity_list.append('%s:数量:%s'%(commodity_name,shop_count))
            print(commodity_list)
    return commodity_list

def price(commodity_list):
    '''计算购物商品总价'''
    total_price=0
    for commodity in shop_list:
        for buy in commodity_list:
            commodity_number, commodity_name, commodity_price = commodity.split(':')
            buy_name,buy_none,buy_count=buy.split(':')
            if commodity_name==buy_name:
                total_price+=int(buy_count)*int(commodity_price)
    print(commodity_list)
    print('商品总价:%s'%total_price)
    return total_price

def recharge(username_enter):
    '''给账户充值'''
    if username_enter==False:
        print('你还没有登入')
    else:
        money=input('请输入你要充值的金额>>>:').strip()
        with open(r'login.txt','r',encoding='utf-8')as f_1,\
            open(r'.login.temp.txt','w',encoding='utf-8')as f_2:
            for line in f_1:
                res=line.strip('\n').split(':')
                if res[0]==username_enter:
                    f_2.write('%s:%s:%s\n'%(res[0],res[1],money))
                    print('充值成功,你充值的金额是%s' % money)
                else:
                    f_2.write('%s\n'%line.strip('\n'))
        os.remove('login.txt')
        os.rename('.login.temp.txt','login.txt')
        f_1.close()
        f_2.close()
        return money

def pay(total_price,username_enter):
    '''支付功能,把充值的钱减去选购商品总价,最后显示余额'''
    remaining_sum=0
    if username_enter==False:
        print('你还没有登入')
    else:
        with open(r'login.txt','r',encoding='utf-8')as f_1,\
            open(r'.login.temp.txt','w',encoding='utf-8')as f_2:
            for line in f_1:
                res=line.strip('\n').split(':')
                if res[0]==username_enter and len(res)<3:
                    print('你的账户还没充值')
                if res[0]==username_enter and len(res)==3:
                    remaining_sum=int(res[2])-int(total_price)#remaining_sum拿到余额
                    f_2.write('%s:%s:%s\n'%(res[0],res[1],remaining_sum))
                    print('付款成功,还剩余额:%s' % remaining_sum)
                else:
                    f_2.write('%s'%line)
        os.remove('login.txt')
        os.rename('.login.temp.txt', 'login.txt')
        f_1.close()
        f_2.close()
        return remaining_sum

username_enter=False
username_login=False
total_price=False
money=False
while True:
    print('''
    功能选择:
    0.退出
    1.注册
    2.登入
    3.购物
    4.充值
    5.支付
    ''')
    num=input('请输入序号>>>:').strip()
    ':break
    ':
        username_login=login()#拿到返回值:注册成功的用户名
    ':
        username_enter=enter()#拿到返回值:登入成功的用户名
    ':
        commodity_list=shop()#拿到返回值:选购商品列表
        total_price=price(commodity_list)#拿到返回值:选购商品的总价
    ':
        money=recharge(username_enter)#拿到返回值:拿到充值的金额
    ':
        pay(total_price,username_enter)
    else:
        print('序号选择错误')

python简单购物车改进版的更多相关文章

  1. Python 简单购物车

    product_list =[ ('huawei',3000), ('hongmiNote3',3000), ('sanxing',2600), ('ThinkPad870',15000), ('Ip ...

  2. python实现简单购物车系统(练习)

    #!Anaconda/anaconda/python #coding: utf-8 #列表练习,实现简单购物车系统 product_lists = [('iphone',5000), ('comput ...

  3. Python实例---简单购物车Demo

    简单购物车Demo # version: python3.2.5 # author: 'FTL1012' # time: 2017/12/7 09:16 product_list = ( ['Java ...

  4. 简单购物车程序(Python)

    #简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...

  5. 用Python实现简单购物车

    作业二:简单购物车# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,# 如果输入为空或其他非法输入则要求用户重新输入 shopping_list = [] w ...

  6. python 简单搭建非阻塞式单进程,select模式,epoll模式服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 可以看我的上篇文章 <python 简单搭建阻塞式单进程,多进程, ...

  7. python 简单搭建阻塞式单进程,多进程,多线程服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...

  8. Python简单爬虫入门三

    我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...

  9. Python简单爬虫入门二

    接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...

随机推荐

  1. Java ME之Android开发从入门到精通

    1. 搭建Android开发环境 方式一:使用ADT插件安装 ADT插件的下载与安装,ADT插件获取网址:http://www.androiddevtools.cn/ 下载好的ADT插件如图所示: 在 ...

  2. vue之综合Demo:打沙袋

    demo7.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1 ...

  3. spring boot和swagger 整合

    本文来源:https://blog.csdn.net/saytime/article/details/74937664 一.依赖 <dependency> <groupId>i ...

  4. 为WebRTC 应用部署Turn Server

    部署WebRTC 或 SIP p2p 方案时经常会遇到p2p 无法穿透的环境, 这时就是TunServer 的用武之地了. 这里我们使用turnserver-0.7.3 下载confuse依赖库 wg ...

  5. 吴恩达deeplearning之CNN—卷积神经网络

    https://blog.csdn.net/ice_actor/article/details/78648780 个人理解: 卷积计算的过程其实是将原始的全连接换成了卷积全连接,每个kernel为对应 ...

  6. 当使用cokie进行数据交互时候,cookie只需存储该对象的id即可不需要存放其他数据;只需在写个接口根据cookie里面的对象id来创建对象

    当使用cokie进行数据交互时候,cookie只需存储该对象的id即可不需要存放其他数据:只需在写个接口根据cookie里面的对象id来创建对象

  7. 2018-南京网络赛icpc-L题(分层最短路)

    题意:给你n个点,m条边的有向带权图,然后你每次可以选<=k条边的边权变成0,问你1到n的最短路: 解题思路:这道题基本上就是原题了呀,bzoj2763(无向图),解法就是拆点跑分层的最短路,比 ...

  8. MySQL参数调优

    目录 连接相关参数 文件相关参数 缓存相关参数 MyISAM参数 InnoDB参数 连接相关参数 max_connections  允许客户端并发连接的最大数量,默认值是151,一般将该参数设置为50 ...

  9. CH0802 占卜DIY

    模拟 没怎么看题..直接deque模拟水过了.. 但是后来回过头看了下题意..如果再次拿到正面朝上的牌,应该是废操作..可能是数据太水了... #include <bits/stdc++.h&g ...

  10. 微信开发使用webstorm&phpstrom 配置

    材料: 下载链接:https://pan.baidu.com/s/1pLn6jFl 密码:fgo5  -----(将其中的wecharCode.jar 下载下来,然后在webStorm 的 File ...