My python list doesn't understand letters 🙁

Question:

my Code doesent understand letters in the list i would like somone to help me fix this

usernames = (BTP, btp, Btp, BTp)
def username(usernames2):
    if usernames == input('whats your username? : ')

Its a simple username system, i plan to use for a interface im making.

Asked By: BryanThePotato

||

Answers:

usernames is defined as a tuple of 4 items, with the names BTP, btp, Btp, and BTp. You said "list" in your title but your code has no actual lists. Lists use brackets, tuples use parentheses.

Anyway, I’m assuming you actually want to check if the user’s input actually was equal to the letters "btp" and you want the check to be case-insensitive, hence why you included all combos of uppercase and lowercase.

The main issue is that you didn’t put quotes around the strings, so you have just 4 bare names sitting in your code which the interpreter expects to have been defined previously. But, you actually don’t have to define all the possible combinations of uppercase and lowercase in the first place – there’s a much easier method to do a case-insensitive string compare, here.

So, your code just needs to look like:

usename = "btp"
def username(usernames2):
    if input('whats your username? : ').lower() == username

Or, if you want to check against multiple usernames, you can use the in operator:

usenames = ["btp", "abc", "foo", "bar"]
def username(usernames2):
    if input('whats your username? : ').lower() in usernames
Answered By: Random Davis

If you haven’t declared BTP, btp, Btp, and BTp you will get a NameError

If you wanted to use strings you need single or double quotation marks:

usernames = ("BTP", "btp", "Btp", "BTp")

With that you create a tuple containing four string elements.

The next issue is with your if condition as you compare if a tuple is equal a string.

Try storing the input given from the user in a variable:

def username(usernames):
    user_input = input('whats your username?: ')
    if user_input in usernames:
        # Do something when username is found
Answered By: Wolric
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.