0
names = []

name = input("What is your name")

for x in names: 
    if x != name:
        print("The name is not on the list")
    else:
        print("The name is on the list")

Is it possible to loop an empty list and if so what is the value output of it when the loop runs? I'm trying to loop the list where if it is empty and the name doesn't match it will print "the name is not on the list" while if it is it will print "the name is on the list".

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Bear25
  • 11
  • 1
  • 1
    Does this answer your question? [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list) – mkrieger1 Mar 27 '21 at 21:43
  • The algorithm you are trying to use is wrong, even if the list is not empty. – mkrieger1 Mar 27 '21 at 21:44
  • 4
    And yes, you can loop over an empty list, but there will be 0 iterations. – mkrieger1 Mar 27 '21 at 21:46

2 Answers2

-1

No, you can't iterate over an empty list because there are no items in it. You should populate the list first and then try the algorithm of yours.

Also, check the indents on your if-else statements. In python we use 4 spaces for that.

Nek1o
  • 1
-1

You can try name.append(input("What is your name")) to add string into your list

In python, you can't use for loop in empty list