Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get day of the week based on inputed date(Python)

So i wanted the user to enter date and based on that day to get named day of the week, for example today’s date is 2022.11.10, so wanted answer would be Thursday.
I know this is wrong, can anybody help?

import datetime

def dayOfTheWeek(day, month, year):
    date = datetime.date(year, month, day)
    weekday = date.weekday()
    day_dict = { 0 : "Monday", 1 : "Tuesday", 2 : "Wednesday", 3 : "Thursday", 4 : "Friday", 5 : "Sturday", 6 : "Sunday"}
    
    for key in day_dict.key():
        if weekday == key:
            return day[key]
        
year = int(input("Year: "))     
month = int(input("Month: "))       
day = int(input("Day: "))

dayOfTheWeek(day, month, year)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Instead of going through the dictionary with for loop, you can simply return the result already converted by dictionary:

import datetime

def dayOfTheWeek(day, month, year):
    date = datetime.date(year, month, day)
    weekday = date.weekday()
    day_dict = { 0 : "Monday", 1 : "Tuesday", 2 : "Wednesday", 3 : "Thursday", 4 : "Friday", 5 : "Sturday", 6 : "Sunday"}

    return day_dict[weekday]
    
year = int(input("Year: "))     
month = int(input("Month: "))       
day = int(input("Day: "))

dayOfTheWeek(day, month, year)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading