Python Forum

Full Version: Help with an if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I’m working through my Python Crash Course book and building one of the “try it yourself” tests.

I don’t understand why the username Admin is being printed in the else statement?

I’m trying to have Admin and Moderator print their own special messages and the other users get the generic ones.

Here is the code:

usernames = ["cozzie", "stylingpat", "lilly25", "moderator", "admin"]

if usernames:
	for username in usernames:
		if "admin" in username:
			print(f"Hello {username}, would you like a status report?")
		if "moderator" in username:
			print(f"Hello {username}, would you like the server messages?")
		else: 
			print(f"Hello {username}, welcome to the server!")
else:
	print("We need to find some users!")
And here is the output I am getting:

Output:
Hello cozzie, welcome to the server! Hello stylingpat, welcome to the server! Hello lilly25, welcome to the server! Hello moderator, would you like the server messages? Hello admin, would you like a status report? Hello admin, welcome to the server!
Any advice? Thank you
Hi stylingpat,

Change your Code from :-

usernames = ["cozzie", "stylingpat", "lilly25", "moderator", "admin"]
 
if usernames:
    for username in usernames:
        if "admin" in username:
            print(f"Hello {username}, would you like a status report?")
        if "moderator" in username:
            print(f"Hello {username}, would you like the server messages?")
        else: 
            print(f"Hello {username}, welcome to the server!")
else:
    print("We need to find some users!")
To :-

usernames = ["cozzie", "stylingpat", "lilly25", "moderator", "admin"]
 
if usernames:
    for username in usernames:
        if "admin" in username:
            print(f"Hello {username}, would you like a status report?")
        elif "moderator" in username:
            print(f"Hello {username}, would you like the server messages?")
        else: 
            print(f"Hello {username}, welcome to the server!")
else:
    print("We need to find some users!")
You needed an elif instead of an if, in the "moderator" line of Code, that fixes the Output, to what you wanted.

Best Regards

Eddie Winch Smile
The if conditionals on lines 5 and 7 are independent. Failing or succeeding on one doesn't affect the other. Perhaps you wanted to use an if/elif pair?

The for loop on line 4 will eventually set username to "admin".
Line 5 will match the condition and you get the status report.
Line 7 is considered and fails, so falls to the else on line 9. That section is then executed.
Yes bowlofred,

Changing the if to an elif, in the "moderator" line of Code, fixes the problem.

Regards

Eddie Winch
Awesome I’ll sit down tomorrow and look through it and make sure I’m understanding the logic flow of the two sets of code!! Thank you!!
        if "admin" in username:
            print(f"Hello {username}, would you like a status report?")
        if "moderator" in username:
            print(f"Hello {username}, would you like the server messages?")
        else: 
            print(f"Hello {username}, welcome to the server!")
This is two, separate, completely unrelated conditions. I'd argue that the spacing hides some of the clarity that could easily be present (just add a newline before if "moderator" in username: to see what I mean).

Rewritten, here's the exact same structure:
        if "admin" in username:
            # user is admin

        if "moderator" in username:
            # user is moderator
        else: 
            # user is not moderator (but can be admin, since that's a different check)
To get the functionality you're expecting, you could use an elif as mentioned before, or you could nest the subsequent conditional within an else. ie:
        if "admin" in username:
            print(f"Hello {username}, would you like a status report?")
        else:
            if "moderator" in username:
                print(f"Hello {username}, would you like the server messages?")
            else: 
                print(f"Hello {username}, welcome to the server!")