首页 > 解决方案 > 如何使我的代码满足此要求

问题描述

问题要求:确保您的比较不区分大小写。如果使用了“jack”,则不应接受“JACK”。(为此,您需要制作一份 current_users 的副本,其中包含所有现有用户的小写版本)[我不知道如何使我的代码符合要求 4]

##Python##

current_users = ['curry', 'james', 'jack', 'durant', 'edward']
new_users = ['Edward', 'Curry', 'stephen', 'jAcK', 'david']
for new_user in new_users:
    if new_user.lower() in current_users:
        print(
            f"the user name {new_user} has been used in as way of {new_user.lower()}, so it is not available"
        )
    else:
        print(f"the user name {new_user} is registered successfully")

标签: python

解决方案


current_users还必须具有小写的所有名称(为此,您需要制作 current_users 的副本,其中包含所有现有用户的小写版本<--为此要求)。我已经为此使用了列表理解

current_users = ['curry', 'james', 'jack', 'durant', 'edward']
new_users = ['Edward', 'Curry', 'stephen', 'jAcK', 'david']
current_users_lower=[i.lower() for i in current_users] 
for new_user in new_users:
    if new_user.lower() in current_users_lower:
        print(
            f"the user name {new_user} has been used in as way of {new_user.lower()}, so it is not available"
        )
    else:
        print(f"the user name {new_user} is registered successfully")

输出:

the user name Edward has been used in as way of edward, so it is not available
the user name Curry has been used in as way of curry, so it is not available
the user name stephen is registered successfully
the user name jAcK has been used in as way of jack, so it is not available
the user name david is registered successfully

推荐阅读