【问题标题】:if x in [list] iterator如果 x 在 [list] 迭代器中
【发布时间】:2018-08-26 03:40:55
【问题描述】:
def new_user() -> str:
    users = []
    print("Welcome to The 'Create New User' Interface")
    sleep(0.5)
    x = input("Enter Name to Use for Account Access\n*Name is Case Sensitive to Access Account*: ")
    if x in users:
        x = input("That User Already Exists! Enter a New Name: ")
        users.append(x)
        print("Your Account Access Name is: " + str(x))
    else:
        users.append(x)
        print("Your Account Access Name is: " + str(x))

所以我不知道如何表达这个问题,但我有这段代码,如您所见,我想检查用户输入的名称是否已经存在,如果存在,它将提示输入新名称并将其添加到列表中,如果它不存在,它会将其添加到列表中,但是有一种解决方法,如果列表已经包含名称并且用户输入了相同的名称 if x in users:代码将运行,当提示输入另一个名称时,如果他们输入相同的名称,它不会识别它已经存在并将其添加到列表中,我该如何防止这种情况发生?

【问题讨论】:

  • 你的函数没有返回任何东西。 users 是一个局部变量。每次调用该函数时,它都会被创建为空。
  • 还有一点,在这种情况下str(x)x 完全相同。为自己保存一个函数调用。
  • 我的错误返回是 x,它用于大部分代码,但我的问题出在此处,对于“用户”的事情,我在函数之外列出了列表,但仍然得到相同结果,在提示输入新名称后,我可以通过输入相同的名称来绕过初始检查

标签: python python-3.x list


【解决方案1】:

要获得有效的用户输入,请将请求包装在一个循环中,并且当您有有效输入 break 时,例如:

print("Enter Name to Use for Account Access")
print("*Name is Case Sensitive to Access Account*")
while True:
    x = input("Enter a Name: ")
    if x not in users:  # Valid input
        break
    print("That User Already Exists!")

users.append(x)
print("Your Account Access Name is:", x)

【讨论】:

    【解决方案2】:

    解决方案

    users = []
    print("Welcome to The 'Create New User' Interface")
    x = input("Enter Name to Use for Account Access\n*Name is Case Sensitive to Access Account*: ")
    while x in users:
        x = input("That User Already Exists! Enter a New Name: ")
    users.append(x)
    print("Your Account Access Name is: " + str(x))
    

    只需将您的 if 循环更改为 while 循环,该循环将一直持续到给出唯一名称为止。

    建议

    users = []
    print("Welcome to The 'Create New User' Interface")
    while True:
        user_name = '' #now users can not enter a empty user_name
        while not user_name:
            user_name = input("Enter Name to Use for Account Access: ")
        for i in range(0, len(users)): #different loop to enable use of lower()
            while  user_name.lower() == users[i].lower(): #removes need for unique cases
                print("That User Already Exists!")
                user_name = '' #again stopping empty fields
                while not user_name:
                    user_name = input("Enter Name to Use for Account Access: ")
        users.append(user_name)     
        print("Your Account Access Name is: " + user_name)
    

    首先,我们可以创建一个循环来拒绝任何空白user_name

    接下来我们可以在检查user_name 是否存在于users[] 时使用.lower()。通过这样做,我们可以保留用户想要用来存储其姓名的唯一大小写格式(可能用于显示目的),但同时我们可以检查user_name 是否已经存在,无论大小写格式如何。

    清理它,我们可以这样做:

     def ask_user(message=''): #create function to check for blank inputs
        user_name = ''
        while not user_name:
            user_name = input(message)
        return user_name
    
    users = []
    print("Welcome to The 'Create New User' Interface")
    while True:
        user_name = ask_user("Enter Name to Use for Account Access: ")
        for i in range(0, len(users)):
            while  user_name.lower() == users[i].lower():
                print("\nThat User Already Exists!") #newline for clarity
                user_name = ask_user("Enter Name to Use for Account Access: ")
        users.append(user_name)     
        print("\nYour Account Access Name is: " + user_name) #newline for clarity
    

    在这里我创建了处理空白输入的ask_user。然后在几个地方添加了\n 以帮助提高可读性。

    输出

    (xenial)vash@localhost:~/pcc/10$ python3 helping.py
    Welcome to The 'Create New User' Interface
    Enter Name to Use for Account Access: 
    Enter Name to Use for Account Access: vash
    
    Your Account Access Name is: vash
    Enter Name to Use for Account Access: VASH
    
    That User Already Exists!
    Enter Name to Use for Account Access: 
    Enter Name to Use for Account Access: p0seidon
    
    Your Account Access Name is: p0seidon
    Enter Name to Use for Account Access: P0SEidoN
    
    That User Already Exists!
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 2020-12-02
      相关资源
      最近更新 更多