【问题标题】:Breaking two nested loops in only one condition仅在一种条件下打破两个嵌套循环
【发布时间】:2022-01-08 14:53:32
【问题描述】:

这是我的代码

login = input("Whats the keyword?\n")

if login == "3faze":
    print("Ok I will give you access!")
    access = True
else:
    print("Wrong! Try opening the program again!")
    t.sleep(5)
    access = False

if access == True:
        while True:
                action = input("What do you want to do?")
                if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
                        print("Ok")
                        while True:
                                which = input("Which password do you want to get?")

                                if which == "google" or which == "Google":
                                    print(str(passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]))
                                    close = input("Do you want to close?")
                                    if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
                                        break
                                        
                                        t.sleep(1)
                                    else:
                                        print("Ok")
                                        t.sleep(1)

在哪里说 break 我想打破两个嵌套循环。 感谢帮助的人。 我是为密码管理器制作的,因为您可能会说那里还有更多代码,但我不想显示它,因为它有我的密码,如果需要,我将制作一个没有这些密码的模板。

【问题讨论】:

标签: python python-3.x python-2.7


【解决方案1】:

您可以将 while True 更改为变量并更改其布尔值以中断

password_found = False

while not password_found:
    ...
    while not password_found:
        close = input()
        if close == 'yes':
            password_found = True

【讨论】:

    【解决方案2】:

    根据您的代码,我尝试进行修改以将两个循环合并为一个。

    您可以根据需要将if close...:中的continue替换为break

    login = input("Whats the keyword?\n")
    
    if login == "3faze":
        print("Ok I will give you access!")
        access = True
    else:
        print("Wrong! Try opening the program again!")
        t.sleep(5)
        access = False
    
    if access is True:
        skip = False
        while True:
            if not skip:
                action = input("What do you want to do?")
                if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
                    print("Ok")
                    skip = True
                else:
                    continue
    
            which = input("Which password do you want to get?")
            if which == "google" or which == "Google":
                print(str(passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]))
                close = input("Do you want to close?")
                if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
                    t.sleep(1)
                    skip = False
                    continue
                    # break
    
                else:
                    print("Ok")
                    t.sleep(1)
    
    

    【讨论】:

      【解决方案3】:

      您可以再添加一个布尔变量,并将中间的while 循环替换为while var_name

          login = input("Whats the keyword?\n")
          
          if login == "3faze":
              print("Ok I will give you access!")
              access = True
          else:
              print("Wrong! Try opening the program again!")
              t.sleep(5)
              access = False
          
          if access == True:
                  leave = False
                  while not leave:
                          action = input("What do you want to do?")
                          if action == "get passwords" or action == "Get Passwords" or action == "Get passwords" or action == "get Passwords":
                                  print("Ok")
                                  while True:
                                          which = input("Which password do you want to get?")
          
                                          if which == "google" or which == "Google":
                                              print(str(passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]))
                                              close = input("Do you want to close?")
                                              if close == "yes" or close == "Yes" or close == "yea" or close == "Yea" or close == "yea":
                                                  leave = True
                                                  
                                                  t.sleep(1)
                                              else:
                                                  print("Ok")
                                                  t.sleep(1)
      

      除此之外,您可以只检查close.lower() == "yes",而不是检查每个string 的可能性(例如Yesyes)。

      【讨论】:

        【解决方案4】:

        还有 2 条提示: 您可以将输入转换为小写,这样您就不必将其与两个版本进行比较,例如:

        如果 action == "获取密码" 或 action == "获取密码" -> if action.lower() == '获取密码'

        1. 有比
        2. 更好的选择
        passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]
        
        

        但不清楚“密码”是什么(字符串、字母列表、数字列表?)

        【讨论】:

          【解决方案5】:

          尝试使用函数并用 return 打破循环。 例如:

          def sample():
              while True:
                  while True:
                      if condition:
                          return
          

          然后使用代码中的函数:

          sample()
          

          【讨论】:

            【解决方案6】:

            我对您的问题的解释,我做了一些清理,使代码对我来说更具可读性,但这是个人决定:

            actionLst = ("get passwords", "Get Passwords", "Get passwords", "get Passwords")
            yesLst = ("yes", "Yes", "yea", "Yea", "yea")
            googleLst = ("google", "Google")
            
            login = input("Whats the keyword?\n")
            
            if login == "3faze":
                print("Ok I will give you access!")
                access = True
            else:
                print("Wrong! Try opening the program again!")
                t.sleep(5)
                access = False
            
            FirstTime = True
            while access: # at this level already invariant , 
                          # don't use it in the loop itself !
                if FirstTime:
                    action = input("What do you want to do?")
                    if action in actionLst: print("Ok") 
                    else: continue
                    FirstTime = False
                else:
                    which = input("Which password do you want to get?")
                    if which in googleLst:
                        print(str(passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]))
                        close = input("Do you want to close?")
                        if close in yesLst:
                            break
                            t.sleep(1)
                        else:
                            print("Ok")
                            t.sleep(1)
                            # if you don't close will loop again !?
                            # so is not bulletproof ...
            

            【讨论】:

              【解决方案7】:

              尝试在外循环的最后一行添加第二个 break 关键字。

              【讨论】:

                猜你喜欢
                • 2012-01-19
                • 1970-01-01
                • 2014-06-11
                • 2014-11-15
                • 2012-03-30
                • 2010-10-13
                相关资源
                最近更新 更多