Cooper Cooper - 3 months ago 8
Python Question

"if not item in list" not functioning properly in Python?

I have a simple program that checks the user's 'passcode' that either allows them access or denies them access.

If the user types in the correct passcode, the print statement "Login successful! Passcode used:", passcode" is printed, however, if the passcode is wrong, it simply asks for their passcode again without printing the statement "Login unsuccessful."...

Why is this? Thankyou.

LoginCorrect = 0
lst = ['1234', '2345', '3456', '4567', '5678', '6789']
while LoginCorrect == 0:
passcode = input("Please enter your passcode: ")
for item in lst:
if item == passcode:
print("Login successful! Passcode used:", passcode)
LoginCorrect = 1
if not item in lst == False:
print("Login unsuccessful.")

Answer

How about a while statement with continue and break?

lst = ['1234', '2345', '3456', '4567', '5678', '6789']

while True:
    passcode = input('Please enter your passcode: ')
    if passcode not in lst:
       continue
    else:
        print("Login successful! Passcode used:", passcode)
        break

You state "if the passcode is wrong, it simply asks for their passcode again without printing the statement "Login unsuccessful."
That being the case, why are you coding for "Login Unsuccessful" ?

Comments