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.
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
Try using a function and break the loops with return. As example:
def sample():
while True:
while True:
if condition:
return
Then use in the code:
sample()
CodePudding user response:
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".
CodePudding user response:
Tysm everyone i forgot that i could do it with the boolean way, the others i didint know, but tysm for everone that helped love yall <3
CodePudding user response:
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'
- 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?)
CodePudding user response:
Try puting a second break keyword in the last line of the outer loop.
