Search code examples
pythonif-statementlogic

Why does the code say good morning even if the hour is 18.24


Why is this still saying good morning if the hour is 18.24?

Code: Picture of the code

Sorry for my bad English!


Solution

  • It says "Good Morning!" because your first conditional checks to see if hour >= 5, in which case, 18.24 >= 5 is indeed True, hence the conditional triggers.

    You want to reorder your conditionals to be in the reverse order. Something like:

    if hour >= 20:
       do something...
    elif hour >= 17:
       do something...
    elif hour >= 14.5:
       do something...
    ...
    

    In this way, it will only trigger when 18.24 >= 17 and only output Good Evening


    In the future, instead of adding code snippets as an image, you can add them as a code block like I did. You can see it in the edit tools.