Greetings Community! Trying to teach myself Python3 and I’m doing an exercise where I’m combining a for loop and an if-elif-else chain. Please excuse my formatting if it comes across as improper…still learning.
#usernames = ['peter', 'susan', 'katrina', 'joe', 'admin']
usernames = 
for username in usernames:
if username == 'admin':
print("Greetings " + username.title() + ", " + "and Welcome!")
elif username in usernames:
print("Hello" + " " + username.title() + "!")
elif len(usernames) == 0:
print("We need to find some users!")
I have the first list hash-tagged to test for the empty list condition. But I can’t get Python to return “We need to find some users!” when the list is empty. I figured I could check the length of the list, which is indeed zero, as a condition, but I’m getting a blank screen when I Execute the code.
I’m using Geany if that makes any difference.
What am I doing wrong?
EDIT: I had proper indentation when I wrote this, but all that went away when I hit post. I’m assuming there’s a way to insert code specifically, so if someone could help me with that as well.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (
</>) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
this is inside the loop, if there are no items in the array, the loop has nothing to iterate on, and doesn’t execute
Is there another way to test for the condition of an empty list while still being inside the loop? Trying to be as concise as I know how. Or do I just test the list with a separate if statement?
Thank-you, JeremyLT, for the information!
well, do you need to check it for each element of the array? or do you need to check it once?
also, your first elif isn’t a bit redundant?
I need to accomplish three things:
- A special message if ‘admin’ is in the list.
- Greeetings to the other elements in the list.
- The aforementioned statement about needing users if the list is empty.
I need to check to see if the list is empty once I suppose. So that would mean I could pull it entirely out of the for loop, right? And seeing as I’m only concerned with elements in the array, yeah…the elif is redundant…but the book I’m following says that else and elif, while not being the same, can achieve the same purpose depending on the situation. Is it proper to just use else in this case, instead of elif?
you can use an “else” if you have only two conditions (if condition is true do the stuff in the if, if it’s false, do the stuff in the else), you need an
elif if you have more than one option for when the condition in the if is false, for example if sometimes you just need to do nothing
I think I have all of this backwards. I could first use an if statement to check the length of the list, then nest a for loop inside of it if the list comes back as not being empty. Sound feasible?
Thanks for you’re input, btw.
I think it’s much more readable if you not nest, but as you are not inside a function, I imagine you can’t break from it, so yeah, your idea of checking the length like that works
On the topic of “readability,” do you have an opinion on any reference material relating to formatting my code? The book I’m in says it will touch upon it later, but I’m trying to accumulate resources. I want to be able to come here and not have my formatting be the first issue to be addressed. All I have is some general information about indentation and not having my code exceed a certain number of characters per line (I’ve seen a couple figures).
python relys on indentation to know what’s nested in what, so that’s really important
the formatting itself, when it’s not part of the language syntax, depends a lot on what style the project you are partecipating in requires; there are a few standards, and if you get an editor with a linter and/or prettify extension, you can enforce them
but for readabilty it’s really important to use descriptive variable names, and some comments to point out important passages in the code