Enter the date of the Python code that tells the day of the week.

Asked 1 years ago, Updated 1 years ago, 42 views

I'm working on a code that tells me the day of the week when I write the date.
First of all,

import datetime 
def print_whichday(year,month,date)
r=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    aday=datetime.date(year,month,date)
    bday=datetime.weekday()
print(r[bady])

I made it like this Since weekday comes out as a number of 0~6, if I set the value in advance as a list, I thought the number would index and print out the desired day value right away, but can you see if there is any correction or the code I made is just imagination?
At first, I used the ifelse syntax, but the code got too long, so I didn't want to see it, so I tried it!

python date

2022-09-21 19:38

2 Answers

It is an era of internationalization, so we need to develop it according to Unicode and locale.

Python is possible through locale settings.

In [5]: d = datetime.date(2019, 5, 21) 

In [6]: d.strftime('%A')                                                        
Out[6]: 'Tuesday'

In [7]: import locale                                                           

In [8]: locale.setlocale(locale.LC_ALL, 'ko_KR.UTF-8')                    
Out[8]: 'ko_KR.UTF-8'

In [9]: d.strftime('%A')                                                       
Out[9]: Tuesday


2022-09-21 19:38

I think it's my first time doing a code review to someone, but I'm saying something that's not enough.

First of all, I think the basic idea is very good. In fact, many libraries are translating the day of the week with your ideas, as many languages offer poor date/time-related translation/localization.

The results of my review are as follows.

import datetime

def print_whichday(year, month, day) :
    r = [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
    aday = datetime.date(year, month, day)
    bday = aday.weekday()
    return r[bday]

print(print_whichday(2019, 5, 20))

To explain by line... (Content + importance of 5 points)

That was presumptuous interference. Have fun coding.


2022-09-21 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.