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()
CodePudding user response:
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.
CodePudding user response:
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()
CodePudding user response:
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
CodePudding user response:
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')