Python Forum
Code with empty list not executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code with empty list not executing
#1
usernames = []
for username in usernames:
    if username == 'admin':
        print("Hello admin, would you like a status report?")
    if username != 'admin':
        print("Hello " + username + ", thank you for logging in again!")
    else:
        print("We need more users!")
Error:
what is expected

Output:
We need more users!
Reply
#2
With an empty list there is nothing to loop
Reply
#3
Supposedly with the list being empty, the last comment should execute. Atleast according to the book, Python Crash Course.
Reply
#4
a for loop iterates over items, if there are no items to iterate, nothing will happen
for username in usernames:
username gets each item from usernames
if there are no items to get it won't be assigned as anything.

You would need an if statement that is outside of the loop that checks for an empty list.
Reply
#5
usernames = []
for username in usernames:
    if username == 'admin':
        print("Hello admin, would you like a status report?")
    if username != 'admin':
        print("Hello " + username + ", thank you for logging in again!")
else:
    print("We need more users!")
It has to be else related to the for loop, not as part of if.`
else part of the loop is executed if no break statement is hit inside the loop.
Note that it doesn't make sense to iterate over users in the list - if the list is not empty it will print something for every user in the list and again will print the else part (because there is no break inside the loop)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
thank you
Reply
#7
Truth value testig

empty list is evaluated as False, thus the else part is executed
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Jun-14-2020, 04:58 PM)buran Wrote:
usernames = []
for username in usernames:
    if username == 'admin':
        print("Hello admin, would you like a status report?")
    if username != 'admin':
        print("Hello " + username + ", thank you for logging in again!")
else:
    print("We need more users!")
It has to be else related to the for loop, not as part of if.`
else part of the loop is executed if no break statement is hit inside the loop.
Note that it doesn't make sense to iterate over users in the list - if the list is not empty it will print something for every user in the list and again will print the else part (because there is no break inside the loop)


Nice!! This one stumped me!!
Reply
#9
This gives he same results.
usernames = []
for username in usernames:
    if username == 'admin':
        print("Hello admin, would you like a status report?")
    if username != 'admin':
        print("Hello " + username + ", thank you for logging in again!")
print("We need more users!")
It doesn't make sense to use for / else unless you also use break.
Reply
#10
(Dec-10-2023, 02:59 AM)deanhystad Wrote: It doesn't make sense to use for / else unless you also use break.
A lot of things don't make sense in this poor example from a book :-) e.g. why 2 separate if, and not if/else, string concatenation, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  set.difference of two list gives empty result wardancer84 4 1,433 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  displaying empty list vlearner 5 1,606 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 2,899 Jan-12-2022, 10:23 PM
Last Post: python_student
  Facing error while executing below Python code ramu4651 1 5,629 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  What is the value after JOINING an empty list? JaneTan 2 5,061 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,078 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 2,867 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,772 Mar-19-2020, 06:08 PM
Last Post: michael1789
  Multiple lambda functions in zipped list not executing psolar 0 1,569 Feb-13-2020, 12:53 PM
Last Post: psolar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020