Why does the first print statement's string gets printed in python?

222 Views Asked by At
while True:
    print("HI , TELL YOUR NAME")
    name = input()
    if name != 'anwar':
        continue
    print("your password , please")
    password=input()
    if password != 'bull':
        break
print("access granted")

So the problem here is when I receive the output, I get the first print statement's string that is

---------------output-------------

HI , TELL YOUR NAME
anwar
your password , please
bull
access granted
HI , TELL YOUR NAME

I dont want the first statement to printed again..

where as I look into my book and I saw that the same program prints perfectly ..

Python 3.6
1   while True:
2       print('Who are you?')
3       name = input()
4       if name != 'Joe':
5           continue
6       print('Hello, Joe. What is the password? (It is a fish.)')
7       password = input()
8       if password == 'swordfish':
9           break
10  print('Access granted.')

and the output is what I want really wanted ........

--------------output----------

Who are you?
Joe
Hello, Joe. What is the password? (It is a fish.)
swordfish
Access granted.

please note that the first statment "who are you?" doesn’t print at the end of the program.

2

There are 2 best solutions below

1
On

The correct code is breaking on the right password, while you are breaking if the password doesn't match. So the it will keep looping until you enter a wrong password

if password != 'bull':

should be

if password == 'bull':

to break the loop on bull

1
On

Because you are testing

if password != 'bull':

while your book example uses == ...