Code with empty list not executing - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Code with empty list not executing (/thread-27637.html) |
Code with empty list not executing - adeana - Jun-14-2020 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!") what is expected
RE: Code with empty list not executing - Yoriz - Jun-14-2020 With an empty list there is nothing to loop RE: Code with empty list not executing - adeana - Jun-14-2020 Supposedly with the list being empty, the last comment should execute. Atleast according to the book, Python Crash Course. RE: Code with empty list not executing - Yoriz - Jun-14-2020 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. RE: Code with empty list not executing - buran - Jun-14-2020 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) RE: Code with empty list not executing - adeana - Jun-14-2020 thank you RE: Code with empty list not executing - buran - Jun-14-2020 Truth value testig empty list is evaluated as False, thus the else part is executed RE: Code with empty list not executing - DangerHiVolt - Dec-10-2023 (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 Nice!! This one stumped me!! RE: Code with empty list not executing - deanhystad - Dec-10-2023 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. RE: Code with empty list not executing - buran - Dec-11-2023 (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. |