-1

I have this question about lists so I created this code as a great example, I want to create a list of names so that when I login it can refer to the list of names listed here and I won't get an "Access Denied". I tried doing it using visual studio code but it still doesn't work, any ideas how to correct this? Or maybe if statements are not the right choice for this kind of code?

def name_login():
    list_of_names = ["Alice", "Bob", "Apple"]

    name = input("Name: ")
    if name in list_of_names:
        print("Welcome back %s" % (name))
        name_login()
    else:
        if len(name) > 0:
            print("Access Denied")
        else:
            print("Invalid Input")

matthias_h
  • 11,356
  • 9
  • 22
  • 40
helpmeplease
  • 71
  • 10

4 Answers4

1

Welcome to StackOverflow!

Here are the problematic lines:

    if name == list_of_names:
        print("Welcome back %s" % (name))
    elif len(name) > 0 and name != list_of_names:

What you actually want is not to check the equality of the list to a string (the == operator), but the inclusion of the string inside the list.

Use in operator.

Here's a tutorial:

UPD 1: and instead of !=, you might want to use not in operator. See the tutorial.

UPD 2: Once you solve your problem, be sure to submit your code to CodeReview Stackexchange. There are ways to improve your code and I think you'll benefit from the experience.

Pavel Vergeev
  • 3,060
  • 1
  • 31
  • 39
1

Think about name == list_of_names : do you want your input to be EQUAL to your list ? I guess no, you want to check for inclusion, which is done with the in operator

if name in list_of_names:

Also

  • no need of name not in list_of_names in the second condition, because it is true for sure (because condition 1 has not been validated)
  • use a while loop, to request a name again

To end with :

def name_login():
    list_of_names = ["Alice", "Bob", "Apple"]
    while True:
        name = input("What is your name: ")
        if name in list_of_names:
            print("Welcome back %s" % (name))
            break
        elif len(name):
            print("Access Denied")
        elif len(name) <= 0:
            print("Invalid Input")
azro
  • 53,056
  • 7
  • 34
  • 70
1
  • name == list_of_names will never be true, just like an apple is never equal to an apple basket. You want name in list_of_names instead.

  • This is actually not sufficient, because input would read the user's string together with the final newline, which is not present in any of the array elements, so no input will possibly match. Use strip() to get rid of it.

  • Similarly, in elif, you probably wanted name not in list_of_names.

  • But, in elif, you know name not in list_of_names, because if it was otherwise, it would have been caught in if and not proceeded to elif, so testing for that is redundant; len(name) > 0 by itself is sufficient

  • However, since a string cannot ever have a negative length, len(name) > 0 is equivalent to len(name) != 0. And that expression happens to have the same truth value in Python as len(name), and, ultimately, name (strings, lists and other containers are falsy if empty, and truthy otherwise)

  • In the next elif, you don't need to check len(name) <= 0, since you already checked for its opposite, so you know it's true at that point. (Also, again, string length can't be negative.)

  • Finally, Python does not have Tail Call Optimisation; so eventually your loop might break as your computer runs out of memory. This is very unlikely in your case as computers these days have large memory and users typically would not have patience (or time!) to redo the loop as many times, but in principle this would be much better written as a loop rather than recursion (though conceptually, your recursion is spot-on, and would be the perfect solution in another language, such as Scheme).

Here's how I'd do it:

list_of_names = ["Alice", "Bob", "Apple"]
while True:
    name = input("Name: ").strip()
    if not name:
        print("Invalid Input")
    elif name in list_of_names:
        print("Welcome back %s" % (name))
        break
    else:
        print("Invalid Input")
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Try this:

def name_login():
    list_of_names = ["Alice", "Bob", "Apple"]

    while True:
        name = input("Name: ")
        if len(name) == 0:
            print("Invalid Input")
            continue

        if name in list_of_names:
            print("Welcome back %s" % (name))
            break
        if name not in list_of_names:
            print("Access Denied")
            continue

name_login()

The problem lies with your code segment:

if name == list_of_names:
    print("Welcome back %s" % (name))
elif len(name) > 0 and name != list_of_names:

To check if an element (in your case a str) exists in a list or not, you need to in operator in Python.

If you use == operator to compare a str and list, it will always return False.

abhiarora
  • 9,743
  • 5
  • 32
  • 57