Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python while loop not working?

Im having trouble with this code

email = input("what email would you like to use?:\n")

print("Email accepted")


name = input("\nWhat user name would you like to use?:\n")
print("User name accepted.")


Pass = input("\nWhat password would you like to use?:\n")
print("Password accepted. ")


login = input("\n1.log in   2.Username/password recovery:\n")

if login != "1" or "2":
    print("test")


if login == "2":
    recovery = input("\nplease enter your email to recover your username/password:\n")
    while recovery != email:
        print("incorrect email")
        recovery = input("\nplease enter your email to recover your username/password:\n")
    if recovery == email:
        print("\n"+name+"\n"+Pass)
        username_login = input("\nUser name:\n")
        password_logn = input("\nPassword:\n")

if login == "1":
     print("\n\t<log in>")
     username_login = input("\nUser name:\n")
     password_logn = input("\nPassword:\n")

     print("\n\t<LOGGED IN>")

specificly this part

if login != 1 or 2:
    print("test")

when i try and input a number or letter that is not 1 or 2 it prints test which is what i want but whe i input 1 or 2 it still prints test with the code 1 or 2 is meant to run. it's kind of hard to explain so it's probaly best if you run it fr yourself. If the lovely people ofthe Treehouse forum could help me with this that would be great :)

2 Answers

Hi Ethan

Your current if statement isn't quite formatted properly but aside from that because the answer provided can not be equal to 1 and 2 one of the conditions will always evaluate to true and run the code.

If you change your if statement to read as below I think it should behave as expected.

if login != 1 and login != 2:
    ...

Hope it helps!

Stephen

Thanks that works like a charm

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Python's spoken language structure (using things like and and or and in) is great until it's not.