代码之家  ›  专栏  ›  技术社区  ›  Compoot

使用布尔标志、逻辑和缩进错误跳出python for循环

  •  -2
  • Compoot  · 技术社区  · 7 年前

    下面的代码试图搜索用户名列表,并在第一次输出时返回输出“foundinindex I”或“Sorry username not Found”。

    usernames=["u1","u2","u3"]
    found=False
    
    while found==False:
      username=input("Enter username:")
      for i in range(len(usernames)):
        if username==usernames[i]:
          found=True
          break
    
    if found==True:
      print("Username found in index:",i)
    else:
      print("Sorry,username not found")
    

    如果用户名是正确的,那么当前的代码似乎可以工作,但是如果使用了错误的数据,例如23234,那么它会重复这个问题,而不会跳转到代码底部的if语句(这就是我想要的)。

    有人能纠正这个代码,并解释一下解决这个问题的最有效方法。这很可能与布尔标志“found”有关,我不明白它为什么不跳出并转到if语句的底部。提前谢谢

    5 回复  |  直到 7 年前
        1
  •  1
  •   Manuel Fedele    7 年前

    你真的需要 while 阻止?

    usernames=["u1","u2","u3"]
    index = 0
    found = False
    
    username = input("Enter username:")
    for i in range(len(usernames)):
      if username == usernames[i]:
        found = True
        index = i
        break
    
    if found:
      print("Username found in index:",index)
    else:
      print("Sorry,username not found")
    
        2
  •  1
  •   DirtyBit    7 年前

    usernames=["u1","u2","u3"]
    
    while True:
      user = input("Enter username: ")    
      if user in usernames:
        print("Username found at Index: {}".format(usernames.index(user)))
        break
      else:
        print("Sorry, username not found. Try again")
    

    编辑

    但如果您必须继续使用当前的for循环方法,请在外部for循环上放置else块,并在找到时中断:

    usernames = ["u1","u2","u3"]
    found = False
    
    while found == False:
      username = input("Enter username: ")
      for i in range(len(usernames)):
        if username == usernames[i]:
            print("Username found at Index: {}".format(i))
            break
      else: # not and indentation error
            print("Sorry, username not found. Try again")
    

    编辑2

    usernames = ["u1","u2","u3"]
    
    while True:
      username = input("Enter username: ")
      for i in range(len(usernames)):
        if username == usernames[i]:
            print("Username found at Index: {}".format(i))
            break
      else: # not and indentation error
            print("Sorry, username not found. Try again")
    

    输出 (在所有情况下):

    Enter username: 2334
    Sorry, username not found. Try again
    Enter username: u2
    Username found at Index: 1
    
        3
  •  0
  •   RedX    7 年前

    if 如果输入了正确的用户名。

    while found == true 位)。

    或者你只问一次,看看是否找到了,因为你需要移除 找到时==真 部分。

    我知道你的意思可能是迪特比特干的: https://stackoverflow.com/a/55183057

        4
  •  0
  •   Vindicar    7 年前

    while found==False: 循环到 found 变成 True .

    另外,如果您想查看列表中是否存在字符串,只需使用 list.index() method :

    username=input("Enter username:")
    try:
        i = usernames.index(username)
    except ValueError:
        print("Sorry,username not found")
    else:
        print("Username found in index:",i)
    
        5
  •  0
  •   Cosmin    7 年前

    更好的版本是这个

    usernames = ["u1", "u2", "u3"]
    
    while True:
        username = input("Enter username:")
    
        if username in usernames:
            print("Username found in index:", usernames.index(username))
            break
        else:
            print("Sorry,username not found")
    

    按要求编辑:

    usernames = ["u1", "u2", "u3"]
    found = False
    
    while found is False:
        found = False
        username = input("Enter username:")
    
        for i in range(len(usernames)):
            if usernames[i] == username:
                print("Username found in index:", i)
                found = True
                break
    
        if found is False:
            print("Sorry,username not found")
    
    推荐文章