简体   繁体   中英

Python time greeting program

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')

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.

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')

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+"!"]))
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/

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM