如何比较python中的用户名(比较列表与标题案例对象)?

时间:2018-01-15 20:31:07

标签: python list comparison lowercase

我在Python 3.6.0中有以下代码。我是一个新手,一直试图让这个代码工作。如果用户输入带有大写字母的相同用户名,我不知道如何让python识别(并拒绝);下面是代码。

如果您能告诉我如何,我将不胜感激,请避免理解,只需向我显示完整长度代码:(自"albert"输入new_user后,我想要拒绝用户名的程序,因为它在标题字母current_user中。

current_user = ["John","Peter","sam","Albert"]
new_user = ["albert"]

for name in new_user:
    if name.lower() in current_user:
        print ("Sorry Username is taken, please enter a new username")
    else:
        print ("Username accepted")

1 个答案:

答案 0 :(得分:0)

这是一种理解真正发挥作用的情况 - 但如果你想做很长的路,一个选择是:

current_user = ["John","Peter","sam","Albert"]
new_user = ["albert", "george", "John", "FRED"]

for new in new_user:
    name_ok = True
    for current in current_user:
        if new.lower() == current.lower():
            print ("Sorry {} is taken, please enter a new username".format(new.lower()))
            name_ok = False
            break
    if name_ok:
        print ("Username {} accepted".format(new.lower()))