模块 , 用户管理系统 , 购物车程序 , 分页显示.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 1.列举你常见的内置函数。
"""
强制转换:int() / str() / list() / dict() / tuple() / set() / bool()
数学相关:sum() / max() / min() / divmod() / float() / abs()
输入输出:input() / print()
其余:len() / open() / type() / id() / range()
"""

# 2.列举你常见的内置模块?
'''
json / getpass / os / sys / random / hashlib / copy / shutil / time
'''

# 3.json序列化时,如何保留中文?
'''
import json
a = '你好'
s = json.dumps(a,ensure_ascii=False)
print(s)
'''

# 4.程序设计:用户管理系统# 功能:# 1.用户注册,提示用户输入用户名和密码,而后获取当前注册时间,最后将用户名、密码、注册时间写入到文件。# 2.用户登陆,只有三次错误机会,一旦错误则冻结帐户(下次启动也没法登陆,提示:用户已经冻结)。"""from datetime import datetimeimport osUSER = {}def user():    '''    注册用户    :return:    '''    username = input('注册用户名:')    with open('4用户注册.txt', 'r', encoding='utf-8') as f:        for line in f:            if line.split('_')[0].strip() == username:                return '账户已存在!'    pwd = input('密码:')    sj = datetime.now().strftime('%Y-%m-%d %H:%M:%S')    msg = '_'.join([username, pwd, sj, True])    with open('4用户注册.txt', 'a', encoding='utf-8') as f:        f.write(msg + '\n')    return '注册成功!'def login():    '''    登录    :return:    '''    while 1:        username = input('用户名:')        with open('4用户注册.txt', 'r', encoding='utf-8') as f2:            status = False            for line in f2:                if line.split('_')[0] == username:                    if line.split('_')[-1].strip() == 'False':                        return '账户已锁定!'                    status = True                    break            if not status:                return '用户不存在!'        pwd = input('密码:')        with open('4用户注册.txt', 'r', encoding='utf-8') as f:            for line in f:                if line.split('_')[0] == username and line.split('_')[1] == pwd:                    USER[username] = 0                    return '登录成功'        if USER.get(username) == None:            USER[username] = 0        if USER[username] < 3:            USER[username] += 1            if not USER[username] < 3:                with open('4用户注册.txt', 'r', encoding='utf-8') as f1,open('4用户注册(改).txt', 'w', encoding='utf-8') as f2:                    for line in f1:                        if line.split('_')[0] == username:                            new_line = line.replace('True', 'False')                            f2.write(new_line)                        else:                            f2.write(line)                os.remove('4用户注册.txt')                os.rename('4用户注册(改).txt', '4用户注册.txt')                return '输入错误超过3次,锁定帐号!'            print('登陆失败请重试!')print(user())print(login())"""# 5.有以下文件,请经过分页的形式将数据展现出来。【文件很是小】# 商品|价格# 飞机|1000# 大炮|2000# 迫击炮|1000# 手枪|123# ...'''def func():    f = open('5-6商品列表.txt', 'r', encoding='utf-8')    a = f.read()                            # 所有读到内存    lst = a.split('\n')    max_page, mowei = divmod(len(lst), 3)     # 最大页,最后一页条数(总条数,每页条数)    if mowei > 0:        max_page += 1    while 1:        user = input('要查看第几页(N/n退出):')        if user.upper() == 'N':            return        if not user.isnumeric() or int(user) not in range(1, max_page+1):            print('输入有误!请从新输入!')            continue        start = (int(user)-1)*3        end = (int(user))*3        data = lst[start+1:end+1]        print(lst[0])        for i in data:            print(i.strip())        if not (int(user)>max_page or int(user)<1):            print('当前第%s页,共%s页.' % (user,max_page))func()'''# 6.有以下文件,请经过分页的形式将数据展现出来。【文件很是大】# 商品|价格# 飞机|1000# 大炮|2000# 迫击炮|1000# 手枪|123"""def func():    f = open('5-6商品列表.txt', 'r', encoding='utf-8')    a = f.readlines()                       # 全部行组成一个列表    print(a)    max_page, mowei = divmod(len(a), 3)     # 最大页,最后一页条数(总条数,每页条数)    if mowei > 0:        max_page += 1    while 1:        user = input('要查看第几页(N/n退出):')        if user.upper() == 'N':            return        if not user.isnumeric() or int(user) not in range(1, max_page+1):            print('输入有误!请从新输入!')            continue        start = (int(user)-1)*3        end = (int(user))*3        data = a[start+1:end+1]        print(a[0].strip())        for i in data:            print(i.strip())        if not (int(user)>max_page or int(user)<1):            print('当前第%s页,共%s页.' % (user,max_page))func()"""# 7.程序设计:购物车# 有以下商品列表 GOODS_LIST,用户能够选择进行购买商品并加入到购物车 SHOPPING_CAR 中且能够选择要购买数量,购买完成以后将购买的所# 有商品写入到文件中【文件格式为:年_月_日.txt】。# 注意:重复购买同一件商品时,只更改购物车中的数量。"""SHOPPING_CAR = {}       # 购物车GOODS_LIST = [    {'id': 1, 'title': '飞机', 'price': 1000},    {'id': 3, 'title': '大炮', 'price': 1000},    {'id': 8, 'title': '迫击炮', 'price': 1000},    {'id': 9, 'title': '手枪', 'price': 1000},]       # 商品列表from datetime import datetimedef shopping():    while 1:        for i in GOODS_LIST:            print('序号:' + str(i['id']) + ' 商品:' + i['title'] + ' 价格:' + str(i['price']))        user = input('请输入序号加入到购物车(N/n退出):')        if user.upper() == 'N':            return '退出'        pand = False        for j in GOODS_LIST:            if int(user) == j['id']:                pand = True                if SHOPPING_CAR.get(j['title']) == None:                    SHOPPING_CAR[j['title']] = 0                break        if not pand:            print('输入错误!')            continue        while 1:            count = input('请选择数量:')            if not count.isdigit():                print('请输入阿拉伯数字!')                continue            SHOPPING_CAR[j['title']] += int(count)            date = datetime.now().strftime('%Y_%m_%d')            with open("%s.txt" % date, 'w', encoding='utf-8') as f:                f.write(str(SHOPPING_CAR))                break        print('已添加进购物车!')        print(SHOPPING_CAR)a = shopping()print(a)"""
相关文章
相关标签/搜索