title: How to I make a code Loop 3 times in Python as well as having a while loop around it
body: <p>Very limited on using python and totally stuck, I've managed to get a while loop running on the code below so that the user can keep entering a code until they put in the correct one.</p>
  
  <p>What I'm now looking to do is add a for loop so that it only asks the user to enter the code (4 wrong digits) 3 times and then locks them out. At the same time it needs a while loop to ensure if the user puts more than or less than 4 digits it continually runs and doesn't lock them out.</p>
  
  <p>I just cant get the for loop and while loop working at the same time and don't know what I'm doing wrong. </p>
  
  <pre><code>user = ("1234")
  
  valid = False
  
  while not valid:
  
     #for i in range (3):  
  
          user = input("Hello, welcome! Please enter a four digit passcode to open the safe: ")
          user = user.upper()          
  
          if user == ("1234") :
              print("You have cracked the code, well done")
              valid = True
              break
  
          if user != ("1234") :
              print ("that is incorrect, please try again")
              valid = False
  
  
  
          elif len(user) &gt; 4:
              print ("That is incorrect, please try again")
              valid = False
  
          elif len(user) &lt; 4:
              print ("That is incorrect, please try again")
              valid = False
          else:
              print ("You have been locked out!! Alarm!!!!!!")
  </code></pre>