-1

I will make a python script that say me the correct weekday. This is what i imagine:

def wishMe():

hour = datetime.datetime.now().hour
if hour >= 0 and hour < 12:
    os.system("say Good Morning")
elif hour >= 12 and hour < 18:
    os.system("say Good Afternoon")
else:
    os.system("say Good Evening")

wishMe()

But with weekdays.

if day >= Monday:
    os.system("say It's Monday")
if day >= Tuesday:
    os.system("say It's Tuesday")

.....

I now there's a simpler way. Like this:

  day = datetime.datetime.now().strftime('%A')
  print(day)

But later i will change it a little bit.

I hope anybody can help me. (and understand what i mean!)

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • What do you mean `But later i will change it a little bit.`? – Phix Nov 30 '21 at 17:01
  • See [date.workday()](https://docs.python.org/3/library/datetime.html#datetime.date.weekday) which returns the day of the week as an integer, where Monday is 0 and Sunday is 6. – jarmod Nov 30 '21 at 17:04
  • Does this answer your question? [How do I get the day of week given a date?](https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date) – Roman Purgstaller Nov 30 '21 at 17:04

3 Answers3

1

You could use string manipulation to combine the output of strftime with the code-segment above. For instance, f-strings to combine the say "It's Tuesday" and output of strftime.

For example: os.system(f"say It is {day}"). Note the f in front of the string denoting that it is a f-string. The curly braces are used to insert values from your program into the string.

You can find more information about f-strings here: https://docs.python.org/3/tutorial/inputoutput.html

Note that the command you are passing to os.system is not compatible across different operating systems. The utility "say" exists in Mac OS, but not Windows and Linux. Also special characters like ' can cause trouble when executed in the shell by os.system.

In addition to strftime there are two functions in the datetime-library called weekday and isoweekday. From the docs:

datetime.weekday()

Return the day of the week as an integer, where Monday is 0 and Sunday is 6. The same as self.date().weekday(). See also isoweekdatetimeday().

datetime.isoweekday()

Return the day of the week as an integer, where Mondatetimeday is 1 and Sunday is 7. The same as self.date().isoweekday(). See also weekday(),

Ref: https://docs.python.org/3/library/datetime.html#datetime.weekday

You could use these to switch based on numbers, or simply do your if/else statements by string-comparison.

Olav Aga
  • 143
  • 10
0
("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")[datetime.datetime.now().weekday()]

datetime.datetime.now() will give the date and .weekday() will give the weekday from 0 to 6, then use that as an index for a tuple with the days

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Hippolippo
  • 803
  • 1
  • 5
  • 28
0

You can try this, add a for loop and a list with the days of the week. Just go through them.

def day():

    week_days = ["Monday","Tuesday","Weednsday","Thursday","Friday","Saturday","Sunday"]
    for day in week_days:
        tday = date.today()
        d1 = tday.strftime("%A")
        print(f"Today is {d1}")
    
day()

The output (for today atleast): "Today is Sunday"

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
kritsos23
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 12 '21 at 14:08
  • @OneCricketeer That's true, sorry about that. – kritsos23 Dec 12 '21 at 15:50
  • Now you're never using `day` loop variable. It'd print the same thing 7 times – OneCricketeer Dec 12 '21 at 15:52
  • 1
    @OneCricketeer and come to think about it, you don't really need a "for" loop. – kritsos23 Dec 12 '21 at 15:53