'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()
Solution 1:[1]
passwd = '1234'
# I added answer = None for the first function call
answer = None
def password():
global passwd
# The global answer ensures I use whatever the latest value of answer is(defined by the return statement at teh end of function)
global answer
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()
# This would return the answer and change the global variable
return answer
password()
Solution 2:[2]
Personnaly speaking, your main problem was the order of using while loops. I have managed to solve your problem with the password function by editing your code and changing the order of while loops and some other stuff:
passwd = '1234'
def password():
global passwd
while True:
answer = input("ENTER THE PASSWORD:\t")
if answer != passwd:
wrongAnswer = input("1. TRY AGAIN.\n2. QUIT\nAnswer(1/2):\t")
while wrongAnswer != "1" and wrongAnswer != "2":
print("Incorrect input.")
wrongAnswer = input("Answer(1/2):\t")
if wrongAnswer == "2":
print("Bye!")
break
else:
print("Password is correct! Goodby")
break
Example output
ENTER THE PASSWORD: 8765
1. TRY AGAIN.
2. QUIT
Answer(1/2): 1
ENTER THE PASSWORD: 8765
1. TRY AGAIN.
2. QUIT
Answer(1/2): 1
ENTER THE PASSWORD: 1234
Password is correct! Goodby
Solution 3:[3]
You have to include an else statement or answer == passwd
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()
else:
print('correct')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kumar Utkarsh |
| Solution 2 | |
| Solution 3 | XenO |
