zoukankan      html  css  js  c++  java
  • 内置函数

    内置函数详细信息:https://docs.python.org/3/library/functions.html?highlight=built#ascii

    1、abs():返回绝对值
    2、all():Return True if bool(x) is True for any x in the iterable.If the iterable is empty, return False.
    3、ascii():返回ASCII码
    4、bin():返回整数的二进制
    5、bool():判断一个数据结构是True还是False
    6、bytes():将里面的字符串变成bytes类型
    7、callable():判断一个对象是否能够调用
    8、complex():求复数
    9、dict():生成一个空字典
    10、dir():返回对象的可调用属性
    11、divmod():返回除法的商和余数
    12、enumerate():返回列表的索引和元素
    13、eval():可以把字符串形式的list,dict,set,tuple,再转换成其原有的数据类型
    14、    exec():把字符串格式的代码,进行解义并执行
    filter():对list、dict、set、tuple等可迭代对象进行过滤
    filter(lambda x:x>10, [0,1,2,3,4,10,11,13,14])
    #过滤出所有大于10的数
    15、float(): 转成浮点
    16、frozenset(): 把一个集合变成不可修改的
    17、globals(): 打印全局作用域里的值
    18、hex(): 返回一个10进制的16进制表示形式
    19、id():查看对象的内存地址
    20、isinstance():判断一个数据结构的类型,比如判断a是不是fronzenset, isinstance(a,frozenset) 返回 True or False
    21、iter(): 把一个数据结构变成迭代器
    22、map():map(lambda x:x**2,[1,2,3,43,45,5,6,]) 输出 [1, 4, 9, 1849, 2025, 25, 36]
    23、max():求最大值
    24、ord():返回ascii的字符对应的10进制数 ord(‘a’) 返回97
    25、round() :可以把小数4舍5入成整数. round(10.15,1) 得10.2
    26、sum():求和,a=[1, 4, 9, 1849, 2025, 25, 36],sum(a) 得3949
    27、zip():可以把2个或多个列表拼成一个, a=[1, 4, 9, 1849, 2025, 25, 36],b = [“a”,”b”,”c”,”d”]

    练习题:

    1.输入用户名密码,正确后登录系统 ,打印

    1. 修改个人信息
    2. 打印个人信息
    3. 修改密码
    

    2.每个选项写一个方法

    3. 当用户选择1时,提示用户选择要修改的字段,根据用户输入对相应字段进行修改

    4.登录时输错3次退出程序

    import os
    
    
    def print_info(info, username):
        '''
        打印个人信息
        :param info: dict
        :param username: str
        :return: None
        '''
        personal_info = info[username]
        user_menu = '''
        ----------------------
        1.Name:   {0}
        2.Age :   {1}
        3.Job :   {2}
        4.Dept:   {3}
        5.Phone:  {4}
        ----------------------
        '''.format(username, personal_info[2], personal_info[3], personal_info[4], personal_info[5])
        print(user_menu)
    
    
    def change_info(info, new_value, user, *args):
        '''
        修改个人信息,并且将修改后的数据存入字典中
        :param info:dict
        :param current_value:str
        :param new_value: str
        :return:
        '''
        num = args[0]
        print(type(num))
        info[user][num] = new_value
        return info
    
    def save_file(current, new):
        f_old_name = "info.txt"
        f_new_name = "{}.new".format(f_old_name)
        f = open(f_old_name, "r", encoding="utf-8")
        f_new = open(f_new_name, "w", encoding="utf-8")
        for line in f:
            if current in line:
                new_line = line.replace(current, new)
            else:
                new_line = line
            f_new.write(new_line)
    
        os.rename(f_new_name,f_old_name)
    
    
    def main():
        '''
        主程序
        :return:
        '''
        menu = '''
        1.打印个人信息
        2.修改个人信息
        3.修改密码
        '''
        info_dict = {}
        f = open("info.txt", "r", encoding="utf-8")
        info_data = f.readlines()
        f.close()
    
        # 将数据存入字典中,并且key为username,去掉首行
        for line in info_data:
            line = line.strip()
            if not line.startswith("#"):
                line_li = line.split(",")
                info_dict[line_li[0]] = line_li
    
    
        count = 0
        while count < 3:
    
            username = input("输入账户名:")
            password = input("输入密码:")
            if username in info_dict and password == info_dict[username][1]:
                print("welcome {}".format(username).center(50,"-"))
                while True:
                    print(menu)
                    choice = input("请选择编号>>>:").strip().lower()
                    if choice == "q":
                        break
                    elif choice == "1":
                        print_info(info_dict, username)
                        continue
                    elif choice == "2":
                        person_num = []
                        for k,value in enumerate(info_dict[username],1):
                            k = str(k)
                            person_num.append(k)
                            print(k,value)
                        while True:
                            # 不能修改名字
                            choice_num = input("选择您要修改的编号:")
                            if choice_num == "1" or choice_num == "2":
                                print("不能修改账户昵称或密码,请重新输入!!")
                                continue
                            elif choice_num in person_num:
                                choice_num = int(choice_num)
                                current_value = input("current value>>>:")
                                if current_value == info_dict[username][choice_num-1]:
                                    new_value = input("new value>>>:")
                                    info_dict = change_info(info_dict, new_value, choice_num - 1,username)
                                    print(info_dict[username])
                                    save_file(current_value,new_value)
                                    break
                                else:
                                    print("current value error")
                                    continue
                            else:
                                print("您的输入不符合规范")
                                continue
                    elif choice == "3":
                        while True:
                            current_password = input("输入旧密码>>>:")
                            if current_password == info_dict[username][1]:
                                new_passwd = input("输入新密码>>>:")
                                change_info(info_dict, new_passwd,username,1)
                                save_file(current_password,new_passwd)
                                print("更改成功,新密码为:{}".format(info_dict[username][1]))
                                break
                            else:
                                print("密码错误,请重新输入")
                                continue
                    else:
                        print("输入不符合规范,请重新输入")
                        continue
                    count = 0
            else:
                count += 1
                print("账户名或密码错误,请重新输入")
                continue
    View Code
  • 相关阅读:
    django之数据库orm
    Python的迭代器和生成器
    xhprof 安装使用
    http_load
    sysbench
    LINUX系统下MySQL 压力测试工具super smack
    apache ab工具
    关于流量升高导致TIME_WAIT增加,MySQL连接大量失败的问题
    mysql5.6优化
    php-fpm超时时间设置request_terminate_timeout分析
  • 原文地址:https://www.cnblogs.com/zrxu/p/11578394.html
Copyright © 2011-2022 走看看