-1

So this is my code

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)

Where it says break i would like to break the two nested loops. Thx for the people who help. Im making this for a password manager, as you can probably tell there is some more code up there but i dont want to show it because it has my passwords, if needed i will make a template without those passwords.

zvi
  • 3,032
  • 1
  • 23
  • 40
  • Does this answer your question? [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – zvi Jan 09 '22 at 15:38

7 Answers7

0

Try using a function and break the loops with return. As example:

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

Then use the function in the code:

sample()
Raj Kumar
  • 1,479
  • 14
  • 19
FormulaRossa
  • 1
  • 1
  • 2
0

You can change the while True to a variable and change its boolean to break out

password_found = False

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

Based on your code, I tried a modification to combine the two loops into one.

You can replace continue in if close...: with break according to your needs.

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)

pppig
  • 1,120
  • 1
  • 5
  • 12
0

You can add one more boolean variable, and replace the middle while loop by 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)

Apart from that, instead of checking for each string possibility (for example Yes or yes), you can just check for close.lower() == "yes".

dor132
  • 81
  • 1
  • 7
0

2 more tips: you can turn your input to lowercase so you dont have to compare it to both versions, for example:

if action == "get passwords" or action == "Get Passwords" -> if action.lower() == 'get passwords'

  1. There are better options than
passwords[0] + passwords[1] + passwords[2] + passwords[3] + passwords[4]

but its unclear what 'passwords' is (a string, a list of letters, list of digits?)

0

My interpretation of your problem, i did some cleaning which made the code a bit more readable for me, but this is a personal decision :

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 ...
baskettaz
  • 704
  • 1
  • 9
-1

Try puting a second break keyword in the last line of the outer loop.