当前位置 博文首页 > 早知晓的博客:【Python 习题总结4】检查用户名

    早知晓的博客:【Python 习题总结4】检查用户名

    作者:[db:作者] 时间:2021-08-03 09:42

    本篇博客对应书中5.4节的动手试一试,以下主要为我做题后的心得笔记。

    练习5-10:检查用户名

    该题目主要是熟悉一下5.4节中提到的在 if 语句中使用多个列表的方法,我贴一下自己写的代码,仅供参考奥。

    #author:早知晓
    #time:2021\07\24
    
    current_users=['A','B','C','D','E']
    new_users=['O','P','Q','a','B']
    current_users01=[]
    for current_user in current_users:
        current_users01.append(current_user.lower())
    print(current_users01)  
    
    for new_user in new_users:
        if new_user.lower() in current_users01:
            print(f"'{new_user}' have exsisted,please input another user-name!")
        else:
            print(f"'{new_user}' is proper!")
    

    运行结果如下图:
    在这里插入图片描述
    注:该习题没有什么问题,唯一需要注意的就是创建列表 current_users 副本时最好使用列表解析,我写代码时直接就用的 for 循环,这样是不好的,要养成良好的编程习惯奥~

    列表解析生成current_users 副本代码如下:

    current_users01=[current_user.lower() for current_user in current_users]
    

    如果各位小伙伴有什么问题或者建议的话,欢迎大家评论区交流呀~
    希望大家都走在开满鲜花的路上,加油鸭~


    版权声明

    原文作者:早知晓
    博文链接:Click here
    转载请注明出处,谢谢合作~


    cs
    下一篇:没有了