Python Forum
Python: why skip the 'else' if password is wrong - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python: why skip the 'else' if password is wrong (/thread-19244.html)



Python: why skip the 'else' if password is wrong - Max_988 - Jun-20-2019

name_expected='Max'
while True:
print( 'Who are you')
name = input()
if (name != name_expected):
continue
print (name_expected +' please enter your password' )
pd= input()
if (pd == '123'):
break
else:
print('password is not right, start from beginning of the loop') ----------> why can not reach here if give a wrong password.
print(' you are granted')


Please help with the above code. If enter a correct name but wrong password, I want to tell wrong password. Thanks!

name_expected='Max'
while True:
    print( 'who are you')
    name = input()
    if (name != name_expected):
        continue
    print (name_expected +' please enter your password' )
    pd= input()
    if (pd == '123'):
        break
    else:
        print('password is not right, start from beginning') # why can not reach here if wrong password
print(' you are granted')              
    
 
Please help with the above code. If I enter a correct name but wrong password, I want to print the else statement above mentioning a wrong password. Thanks!


RE: Python: why skip the 'else' if password is wrong - woooee - Jun-20-2019

Works fine for me. breaks are unreliable as it's somewhat difficult to tell which level you are breaking from. return from a function instead.