title: how do i stop the looping output of the else statement until the value is found in a text file?
body: <p>The code below is a summer project that i have been working on:</p>
  
  <pre><code>from time import sleep
  
  def valrep(x,change): #function goes through a file and replaces specific values
      with open('users.txt', 'r') as file :
          filedata = file.read()
      for aline in open("users.txt","r").readlines():
          word = aline.split()
          if (word[0] == username) and (word[1] == user_password):
              aline2 = aline.replace(word[x], change)
              filedata = filedata.replace(aline,aline2)
      with open('users.txt', 'w') as file:
          file.write(filedata)
          print("SUCCESS")
  
  loop = False
  while loop == False:
      username = input("ENTER A VALID USERNAME&gt;&gt;&gt; ")#splits the input into two seperate variables
      user_password = input("ENTER A VALID 4 DIGIT PIN&gt;&gt;&gt; ")
      valnums = '1234567890' #gives a set of valid characters (used later in the program)
      with open("users.txt","r") as user_file:
          for aline in user_file.readlines():
              vals = aline.split()
              if (vals[0] == username) and (vals[1] == user_password):#goes through the text file looking for the valid username and password
                  loop2 = False
                  while loop2 == False:
                      sleep(3.0) #delays the time for menu to appear
                      print("\n"*300,"="*30,"\n\n\nWELCOME TO HAYDON BANK, {} \n\n\n".format(username),"="*30,"\n  WHICH SERVICE DO YOU REQUIRE?\n","="*30,"\n\n\n    Option 1: View Balance\n\n    Option 2: Withdrawal\n\n    Option 3: Change PIN\n\n\n","="*30,"\n")#this is the menu
                      balance = vals[2] #sets respective part of the file as the balance variable
                      print("\n"*6)
                      option = input("INPUT OPTION PRESS 'Q' TO QUIT&gt;&gt;&gt; ")
                      if option == '1':
                          print("£",balance)
                          sleep(2.0)
                      elif option == '2':
                          withdrawal = input("SELECT AMOUNT TO WITHDRAW:\n £10\n £20\n £50\n £100\n OR INPUT AN AMOUNT THAT IS DIVISIBLE BY 10\n\n\nINPUT AMOUNT&gt;&gt;&gt; £")
                          if withdrawal%10 == 0:
                              print("You selected:£",withdrawal)
                              newbalance = float(balance) - float(withdrawal)#sets the newbalance variable
                              valrep(2,str(newbalance))
                              print("New Balance:£",newbalance)
                              sleep(2.0)
                      elif option == '3':
                          loop3 = True
                          while loop3 == True:
                              old_pass, new_pass = input("input old PIN and new PIN: ").split(" ")
                              if len(new_pass) != 4: #checks if new_pass is 4 characters long
                                  print("INCORRECT LENGTH")
                              elif False in [c in valnums for c in new_pass]: #loops through valnums and compares the variable new_pass to have valid characters   
                                  print("INVALID CHARACTERS")
                              elif old_pass == user_password: #checks if old_pass is equal to user_password
                                  valrep(1,new_pass)
                                  print("New PIN: ",new_pass)
                                  loop3 = False
                                  sleep(2.0)
                      elif option.lower() == 'q':
                          loop2 = True
                          print("Bye", username)
                          sleep(5.0)
                      else:
                          print("INVALID INPUT")
              else:
                  print("INVALID INPUT")
                  break
  </code></pre>
  
  <p>users.txt file:</p>
  
  <pre><code>user0001 1234 123.45
  user0002 1234 123.45
  user0003 1234 123.45
  ...
  user9999 1234 123.45
  </code></pre>
  
  <p>the problem is that when i input values that are closer to line 9999 in the text file in username or user_password variables, it gives me this output:</p>
  
  <pre><code>"INVALID INPUT"
  "INVALID INPUT"
  "INVALID INPUT"
  ...
  </code></pre>
  
  <p>until the code has found the values in the text file.</p>
  
  <p>but i want the output to be like this:</p>
  
  <pre><code>ENTER A VALID USERNAME&gt;&gt;&gt; user9999
  ENTER A VALID 4 DIGIT PIN&gt;&gt;&gt; 1234
  
   ============================== 
  
  
  WELCOME TO HAYDON BANK, user0001 
  
  
   ============================== 
    WHICH SERVICE DO YOU REQUIRE?
   ============================== 
  
  
      Option 1: View Balance
  
      Option 2: Withdrawal
  
      Option 3: Change PIN
  
  
   ============================== 
  
  
  
  
  
  INPUT OPTION PRESS 'Q' TO QUIT&gt;&gt;&gt; 
  </code></pre>
  
  <p>how do i do this?</p>
  
  <p>note: i am not a REAL programmer, im just a kid that knows most of the basics of python coding and <strong>no other type of coding</strong></p>