Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The != statement in the while loop is returning True even when it shouldn't – Python

In the password function, I am trying to make the user enter a password and if he/she inputs the wrong password, then give them another chance to write the correct password or just simply quit.
The problem which I am facing is if I enter the wrong password, and then if i use TRY AGAIN, then even if I write the correct password(i.e, 1234), it still says INCORRECT PASSWORD!

To check what was wrong, I used print(answer, type(answer)) and print(passwd, type(passwd)) to see if they were actually the exact same or not (which they were btw). I also used print(answer != passwd) to see if this was causing the issue and it was. It is returning True even after both answer and passwd are equal. Please suggest what I should do.

passwd = '1234'


def password():
    global passwd
    answer = input("ENTER THE PASSWORD:\t")
    print(answer, type(answer))
    print(passwd, type(passwd))
    while answer != passwd:
        print(answer != passwd)
        print('INCORRECT PASSWORD!')
        print("1. TRY AGAIN.\n2. QUIT")
        ans = input("Answer(1/2):\t")
        while ans not in ['1', '2']:
            print("Incorrect input.")
            ans = input("Answer(1/2):\t")
        if ans == '1':
            password()
        elif ans == '2':
            print("Bye!")
            quit()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

        password()

This does not mean "go back to the start of the function". It means "call the function again, as a completely new action; let it use its own completely separate set of local variables; figure out what is returned; and then continue from this point after it returns".

In other words: it means the exact same thing that it would mean if you called any other function.

When the new function call happens, it prompts for input again and you give it matching input. That means the while loop does not run, and the end of the function is reached – for that call.

Then it returns back to where it was in the old function call (just like it would if any other function had been called), with no effect on the local answer. So now answer != passwd because answer != passwd before, and neither value changed. passwd is global, but answer is local – and the local value is local to this call of the function.

You should try to rethink your logic, draw a flowchart, and re-write it without the recursive call. Make sure you understand how break and continue work, as you may find them useful.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading