Last active
October 17, 2022 22:44
-
-
Save rbw/cd9ce89398a08e2f6b2acab14e3c58d7 to your computer and use it in GitHub Desktop.
Get part of day (morning, afternoon, evening, night) in Python3.6+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def get_part_of_day(h): | |
return ( | |
"morning" | |
if 5 <= h <= 11 | |
else "afternoon" | |
if 12 <= h <= 17 | |
else "evening" | |
if 18 <= h <= 22 | |
else "night" | |
) | |
# To use current hour: | |
# from datetime import datetime | |
# part = get_part_of_day(datetime.now().hour) | |
# print(f"Have a good {part}!") | |
for hour in range(0, 24): | |
part = get_part_of_day(hour) | |
print(f"hour {hour} is {part}") |
you saved me a headache and a half 🤝
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks it's really helpful!