Python Forum
Simple code not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple code not working properly
#1
RESOLVED Hello Python community,

I am currently learning Python, so forgive me if this question is stupid or something really obvious. I was doing a task in the Python Crash Course book, it is a simple if-else syntax, which for some reason doesn't work. I even asked multiple AIs, but they couldn't solve this issue. Here is the code:

usernames = []


if usernames:
    for username in usernames:
        if username == "admin":
            print(f"Welcome, Admin, do you wish to check the analytics?")
        elif username in usernames:
            print(f"Welcome, {username}, thanks for using our service.")
else:
    print("The list is empty!")
As you see, the usernames list is empty, but the print function in the else statement does not work. The output shows absolutely nothing. To make it clear, I always use the same device to code stuff and it works without any issues, and I am using the latest Python version.

Big thanks to everyone who is ready to help.
Reply
#2
if not usernames:
   print('empty list')
else:
    print(usernames)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
I don't understand the problem. When I run the posted code it prints "The list is empty!". Isn't that what you expect to see?

Maybe the indenting got fixed when you submitted your code. Did it look like this on your computer?
if usernames:
    for username in usernames:
        if username == "admin":
            print(f"Welcome, Admin, do you wish to check the analytics?")
        elif username in usernames:
            print(f"Welcome, {username}, thanks for using our service.")
    else:
        print("The list is empty!")
This code prints nothing if usernames is empty. In a for/else loop the else code is only executed if you break out of the loop.

This code does nothing useful.
elif username in usernames:
username is guaranteed to be in usernames. There is no reason to test.

There is no need for the f"" string in this command. You are not printing any variable values.
print(f"Welcome, Admin, do you wish to check the analytics?")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code isnt working. nezercat33 1 657 Mar-14-2025, 03:45 AM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 544 Feb-13-2025, 06:13 PM
Last Post: lminc123
  my code is not working erTurko 1 665 Nov-11-2024, 08:43 AM
Last Post: buran
  Can't get graph code to work properly. KDDDC2DS 1 657 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  Excel isnt working properly after python function is started IchNar 2 1,256 May-01-2024, 06:43 PM
Last Post: IchNar
  Help with simple code JacobSkinner 1 1,349 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  File Handling not working properly TheLummen 8 3,767 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 1,263 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Simple conditional not working as expected return2sender 8 2,400 Aug-27-2023, 10:39 PM
Last Post: return2sender
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 2,348 Jul-22-2023, 10:21 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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