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
#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


Messages In This Thread
Code with empty list not executing - by adeana - Jun-14-2020, 04:02 PM
RE: Code with empty list not executing - by Yoriz - Jun-14-2020, 04:16 PM
RE: Code with empty list not executing - by adeana - Jun-14-2020, 04:30 PM
RE: Code with empty list not executing - by Yoriz - Jun-14-2020, 04:52 PM
RE: Code with empty list not executing - by buran - Jun-14-2020, 04:58 PM
RE: Code with empty list not executing - by adeana - Jun-14-2020, 06:05 PM
RE: Code with empty list not executing - by buran - Jun-14-2020, 06:07 PM
RE: Code with empty list not executing - by buran - Dec-11-2023, 08:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error while executing the Python code in Linux DivAsh 8 1,742 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  set.difference of two list gives empty result wardancer84 4 1,557 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  displaying empty list vlearner 5 1,722 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 3,112 Jan-12-2022, 10:23 PM
Last Post: python_student
  Facing error while executing below Python code ramu4651 1 5,795 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  What is the value after JOINING an empty list? JaneTan 2 5,286 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,152 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 2,966 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,842 Mar-19-2020, 06:08 PM
Last Post: michael1789
  Multiple lambda functions in zipped list not executing psolar 0 1,622 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