Python time greeting program

Question:

I’m trying to create a program that greats users base on the time of the day but when I run my code I get this error:
unorderable types: str() < int()

I dont think I’m doing it the right way and I cant figure out a better way to do it, so are the better ways to write this program???

Here is my Code:

import time
currentTime = time.strftime('%H:%M')   

if currentTime.hour < 12 :
     print('Good morning')
if currentTime.hour > 12 :
     print('Good afternoon')
if currentTime.hour > 6 :
     print('Good evening')
Asked By: Nathan Siafa

||

Answers:

There isn’t an attribute called hour in string variable.

import time
currentTime = int(time.strftime('%H'))   

if currentTime < 12 :
     print('Good morning')
if currentTime > 12 :
     print('Good afternoon')
if currentTime > 6 :
     print('Good evening')
Answered By: Avinash Raj

It looks like you want to work with objects that represent the time. I recommend the datetime module.

Also, your code assumes that the computer will guess whether the hour you entered is AM or PM. You would have to use an hour of 18 to represent 6:00 PM.

>>> import datetime
>>> currentTime = datetime.datetime.now()
>>> currentTime.hour
0
>>> if currentTime.hour < 12:
...     print('Good morning.')
... elif 12 <= currentTime.hour < 18:
...     print('Good afternoon.')
... else:
...     print('Good evening.')
...
Good morning.
Answered By: TigerhawkT3

I saw that the answers listed here were quite lengthy so created a more concise version for those that need it (even though this post is more than 2 years old).
However this top version may break the formatting outlined in PEP-8 because the third line goes over 80 characters (92), so feel free to use the longer version.

import datetime
hour = datetime.datetime.now().hour
greeting = "Good morning" if 5<=hour<12 else "Good afternoon" if hour<18 else "Good evening"

Then use this wherever you need it…

print("{}!".format(greeting))

…or broken down for readability…

import datetime
now = datetime.datetime.now()
hour = now.hour

if hour < 12:
    greeting = "Good morning"
elif hour < 18:
    greeting = "Good afternoon"
else:
    greeting = "Good night"

print("{}!".format(greeting))

An example usage case would be saying a random ‘goodbye’ to make the program seem more lifelike. This would be done something like this…

import random, datetime
hour = datetime.datetime.now().hour
greeting = "Have a nice day" if hour<20 else "Good night"
print(random.choice(["I look forward to our next meeting!",greeting+"!"]))
Answered By: Adil
a=input("Enter your name:")

import datetime
currentTime = datetime.datetime.now()
currentTime.hour

if currentTime.hour < 12:
    print('Good morning',a)
elif 12 <= currentTime.hour < 18:
    print('Good afternoon',a)
else:
    print('Good evening',a)

[write this without using python,click this link provided below to write]

https://colab.research.google.com/

Answered By: venkat

Hope it is not too late to give my contribution to this question:

from datetime import datetime

current_hour = int(datetime.now().strftime('%H'))
if current_hour<12:
    print('Good morning')
elif 12<=current_hour<18:
    print('Good afternoon')
else:
    print('Good Evening')
Answered By: Lex

The required functionality can be also acheived in a single line as follows

>>> import datetime
>>> print(f"Good {['morning', 'morning', 'afternoon', 'evening'][int(datetime.datetime.now().hour/6)]}")
Good morning

Here, the hour is derived from datetime.datetime.now().hour and then used to determine the phase of the day.

The day is divided in four phases in which the first two phases are termed as morning, third as afternoon and fourth as evening.

Answered By: Sanket Dalvi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.