0

Here's a piece of code from my project software:

def fun_1(self, i):
    print("")
    print("Welcome to Option 1: View Passwords")
    while True:
        print("")
        which_o1 = input("1: Input a New Account details \n2: Exit \nPlease Input the option number: ")

        if which_o1 == str(1):
            with open(str(i)+'.txt', 'a+') as file:
                while True:
                    print("")
                    web_n = input("Please Input Website name: ")
                    print("")
                    e_u = input("Please input email/username: ")
                    print("")
                    pass_w = input("Please input password: ")

                    while True:
                        print("")
                        sure = input("Website- " +web_n+"\nEmail/Username- "+e_u+"\nPassword- "+pass_w+"\nAre You sure about these details? Yes/No: ")

                        if (sure.lower()[0]) != 'y' and (sure.lower()[0]) != 'n':
                            print("")
                            print("Please input a valid response Yes/No!")
                            continue

                        elif (sure.lower()[0]) == 'y' and (sure.lower()[0]) != 'n':
                            list_log = [web_n, e_u, pass_w]
                            file.write(str(list_log) + '\n')
                            break
                            break
                            continue

                        elif (sure.lower()[0]) == 'n' and (sure.lower()[0]) != 'y':
                            break
                            continue

        elif which_o1 == str(2):
            return (i)
        else:
            print("")
            print("Please Enter a Valid Response!")
            continue

So has you can see that it has 3 while True loop. The problem is occurring while breaking and looping the loop. If you see the latest While True under "pass_w" in the middle elif, where it says elif (sure.lower()[0]) == 'y' and (sure.lower()[0]) != 'n':, in it I have 2 break and 1 continue because what I wanted to do is that when that elif executes it just break middle 3rd while true, 2nd while true and continue means loop the first while true at the start of the code, but it just keep looping 3rd While True in the middle of the code instead of breaking it.

Is there a way I can make it possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Titler
  • 15
  • 3

1 Answers1

0

Firstly, understand that no lines can be executed after a break statement inside of a while loop. So putting multiple breaks and a continue won't work. You need to restructure your code. Personally, I would recommend either implementing try except statements, or putting some of the code inside of functions so that you can return when you want to stop looping and pass variables as indications to the outer loops where the function was called.

Another option could be instead of using breaks and continues, use a variable or set of variables that your while loops check for to decide if they should continue. So inside your ifs and elifs you could set exitfirstloop = True, etc. and in the while loops you check while not exitfirstloop

Joe D
  • 378
  • 1
  • 9
  • Thanks, man really helped me allot!!:D I thought I was doomed and have to change whole project structure!!:D Thank you very much!!:D – Titler May 07 '17 at 04:19