Python Loops(self Practice problem)

Hello Friends,
I am trying to make few problems for practice, I want to make a password checking progrramme. Following is my 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!!!")
  

<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



Replace this line with your code. 


Ok, I’ll have to dissect this for you, since I too had a similar problem once.

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

This is how I would write it:

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

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.

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.

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.

1 Like

Let’s look at pseudo code to explore this idea…

do 
    input user text
while
    user text is not correct

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,

while True:
    # code
    if some_condition: break
    # code

The two approaches closely parallel one another, as expected.

1 Like

Thanks for your explanation.Can you point my mistake in this 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!!!")

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.

Unreachable after break.

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