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


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 DangerHiVolt - Dec-10-2023, 01:23 AM
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
  what to return for an empty list Skaperen 2 261 May-24-2024, 05:17 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,902 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  set.difference of two list gives empty result wardancer84 4 1,611 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  displaying empty list vlearner 5 1,776 Jan-19-2022, 09:12 AM
Last Post: perfringo
  Remove empty keys in a python list python_student 7 3,246 Jan-12-2022, 10:23 PM
Last Post: python_student
  Facing error while executing below Python code ramu4651 1 5,871 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  What is the value after JOINING an empty list? JaneTan 2 5,420 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  Printing empty list? hhydration 2 2,186 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Stop a function if the list it needs is empty Pedroski55 2 3,022 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Need help to make an empty list with possibility to add Arnsol 1 1,871 Mar-19-2020, 06:08 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

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