0

Simple question here, made a little program asks the user to create a username and password. Every thing worked until I added this "if user" statement. It should restart the loop if I enter 'x' or 'X' but it restarts it regardless of what I enter. What's happening here?

db = {}
num_of_entries = 0


def addToDict(a, b):
    db[a] = b
    print(f"User created: '{a}'")


def removeFromDict(key):
    del db[key]
    print()
    print(f"User '{key}' has been removed.")


while True:
    clear = "\n" * 100
    print()
    print("""
Hi there!     
Would you like to create a new account or delete an existing one?
          ('Create' to create, 'Delete' to delete)
""")

    choice = input("> ").upper()

    if choice == 'CREATE':
        print(f'{choice} mode selected.')
        print()
        user = input("Please enter a username: ")
        if user == 'X' or 'x':
            continue
        else:
            if user not in db:
                passW = input("Please enter a password: ")
                print(clear)
                print()
                addToDict(user, passW)
  • 3
    Change that to: `if user == 'X' or user == 'x':`. This is a common gotcha. – mechanical_meat Mar 04 '21 at 03:09
  • you could also do `if user.lower() == 'x':` (I typically use lower/upper when doing string comparisons to always save a headache) – Fiddle Freak Mar 04 '21 at 03:15
  • 1
    Does this answer your question? [Why does \`a == b or c or d\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Gino Mempin Mar 04 '21 at 03:17

1 Answers1

0

The problem here is that you are posing two separate conditions user == 'X' and 'x'. since 'x' is not null or false it will always be true making the if statement always true as it uses an or operator between the two conditions.

What you need to do as is suggested above is:

if user == 'X' or user == 'x':

You could also do:

if user.lower() == 'x':

Or even:

if user in ['X', 'x']: