1

I'm trying to create a hello world program that imports date, time, and the current username of the logged in user, then displays this like "Good Morning/Afternoon/Evening, CurrentUser" but I'm not sure how to get the username to display. so far I'm not sure what to import to check for the username.

P.S. What i'm looking for is how after importing the User name and date how to implement it in a >>> Print() function.

like this:

Print(Hello, Username)

Shane
  • 13
  • 3
  • 3
    Possible duplicate of [Is there a portable way to get the current username in Python?](https://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python) – Mureinik Oct 07 '17 at 16:56

1 Answers1

0

Get the username:

import getpass

def get_username():
    return getpass.getuser()

Get the time and determine phase of day:

from datetime import datetime

# Define when certain day phases begin
EVENING = 18
AFTERNOON = 12

def get_day_phase(time_now):
    hour_of_day = time_now.hour
    if hour_of_day > EVENING:
        return 'Evening'
    elif hour_of_day > AFTERNOON:
        return 'Afternoon'
    else:
        return 'Morning'

Get the date and time (in a pretty format):

def get_pretty_datetime(now):
    return now.strftime('[%d, %b %Y | %H:%M]') # e.g [09, Oct 2017 | 16:09]

Now to mix it all together!

now = datetime.now()
print(get_pretty_datetime(now), 'Good', get_day_phase(now), get_username() )

Which prints something like this:

[09, Oct 2017 | 11:15] Good Morning Splatmistro
Stefan Collier
  • 2,761
  • 21
  • 29