@digitalace48666 posted a new topic, subject "Python Loops(self Practice problem)". It read:

   Hello Friends,  
    I am trying to make few problems for practice, I want to make a password checking progrramme. Following is my code
    [code] 
        print ("Hello, Welcome to my world!!!")
        while True:
          username=str((input("Please enter your user name:- ")))
          if (username!="USRNM"):
            continue
          password=str(input("Hello User,Please enter your password:- "))
          if (password == "PSSWRD"):
            break
          print ("Access Granted! Welcome To my special area!!!")
          
        
    [/code]
    
    <Below this line, add a link to the EXACT exercise that you are stuck at.>
    
    My code for password check does not work correctly.It accepts all strings.  
    Can someone point my mistake.I am using Python3 From a cloud based site called https://repl.it/JDSO/2  
    Thanks in advance
    [code] 
        
        Replace this line with your code. 
        
        
        
    [/code]

After which, @blackk100 added

    Ok, I'll have to dissect this for you, since I too had a similar problem once.
    [code] 
        print ("Hello, Welcome to my world!!!")  # No syntax problem here
        while True:  # No syntax problem here
          username=str((input("Please enter your user name:- ")))  # the str() function is extra, input() always returns a string, no matter what is the input data
          if (username!="USRNM"):  # No syntax problem here either (you can remove the paranthesis if you want to though)
            continue  # I'm assuming you only want "USRNM" as an input for username, so no issues here either
          password=str(input("Hello User,Please enter your password:- "))  # Again, the str() function is extra
          if (password == "PSSWRD"):  # No syntax problems here
            break  # This kills the loop, and also the program is password is "PSSWRD" (see next line why)
          print ("Access Granted! Welcome To my special area!!!")  # Since this is within the loop, everytime you input the correct username and password, the loop breaks and you never print this. It  prints only when the username is correct, but the password is incorrect
        
    [/code]
    
    This is how I would write it:
    [code] 
        print ("Hello, Welcome to my world!!!")
        flag_1, flag_2 = True, True  # Placeholder boolean variables
        while flag_1:
          username = input("Please enter your user name:- ")
          if username == "USRNM":
            while flag_2:  # I assume you want to keep checking the password once the username is correct. If not, remove this line and variable flag_2, and also reduce the indent on the next lines,
              password = input("Hello User,Please enter your password:- ")
              if (password == "PSSWRD"):
                print ("Access Granted! Welcome To my special area!!!")
                flag_1, flag_2 = False, False  # So that you can kill the loop
        
    [/code]
    
    Notice that I haven't put in the `continue` in an else for both `if`-s, since whenever the condition is false, the way the program is written will make it continue the loop for another iteration.

After which, @digitalace48666 added

    Thanks for your reply, But I am trying this code to get mastery over all the concepts of Python. That's why I tried to use continue+break statement.I considered to code using the placeholder form, But I get confused by this “While True” statement.and I want to code as many programmed to get command over While loops That's why I am trying to code the above manner.This code is for practice purposes only.

To which @mtf replied

    digitalace48666:
    
    > “While True”
    
    Python does not have a `do..while` construct so we use this one instead. The block must contain a `break` keyword at some point or the loop will run forever.

To which @mtf replied

    Let's look at pseudo code to explore this idea…
    [code] 
        do 
            input user text
        while
            user text is not correct
        
    [/code]
    
    This loop begins with no entry condition. It just started once encountered. The code inside the block executes before the condition is examined. Welcome to, ‘at least' as a programming concept, not just math's ‘at least'.
    
    In Python we simulate this construct with,
    [code] 
        while True:
            # code
            if some_condition: break
            # code
        
    [/code]
    
    The two approaches closely parallel one another, as expected.

@digitalace48666 replied

    Thanks for your explanation.Can you point my mistake in this code
    [code] 
        print ("Hello, Welcome to my world!!!")
        while True:
          username=str((input("Please enter your user name:- ")))
          if (username!="USRNM"):
            continue
          password=str(input("Hello User,Please enter your password:- "))
          if (password == "PSSWRD"):
            break
          print ("Access Granted! Welcome To my special area!!!")
        
    [/code]
    
    The code grants access to all entries.Why is this happening? I thought the break statement function is to come out of while loop only if “if condition” is true.

@mtf replied

    digitalace48666:
    
    > print (“Access Granted! Welcome To my special area!!!”)
    
    Unreachable after `break`.

Then @system said

    This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://discuss.codecademy.com/t/python-loops-self-practice-problem/138751', 'title': 'Python Loops(self Practice problem)', 'views': 895, 'has_accepted_answer': False, 'word_count': 769, 'like_count': 2, 'reply_count': 4, 'participant_count': 3, 'posts': 8, 'tags': 'python', 'author': '@digitalace48666', 'participants': '@digitalace48666, @blackk100, @mtf, @system', 'image_url': [None], 'created_at': '2017-06-27T05:11:20.791Z', 'last_posted_at': '2017-07-04T06:18:15.514Z'}