How to find day of the week for each date of that month if the day of first date is given?

I'm a noob who just started learning programming in python. Terribly stuck at this and don't know where to start working on this. I need to find the day of each other day of a month if the first day is Friday of that month. Need to write a function named returnDay which will take one parameter that is the date of that month. The date should be in range of 1 and 31. When I input a date of the month, then have to call that function which returns it's day. If the actual parameter is less than 1 or greater than 31, give a hint that the input isn't available date of that month. **So my question is how to put the 1-31 range here and what is the issue that it shows name 'date' is not defined whenever I run this?

  def returnDay(date):
     day_names= ['Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
     day= input("Enter first day of the month: ")
     date = int(input('please enter the date of October: '))
     i = date%7 -1 
     if day in day_names:
            j = day_names.index(day)+i
            if j >= 7:
                j = j - 7


1 answer

  • answered 2021-10-23 12:43 Brakke Baviaan

    Good day,

    I see a couple of problems in your code. From the way you are calling returnDay(date), it looks like you want to have the date as a parameter to this function. You cannot retrieve the date as input from the user, from within the same function that also needs the date as an argument. So first rewrite your code so that you set the input from outside this function scope, and then supply it to your function for processing. The same goes for the integer part of your date. Now for the logic behind calculating the day to return:

    1. A user supplies the first day
    2. A user supplies the current date

    let's go

        def returnDay(firstDay,currentDate):
        days_in_week = ['Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday', 
        'Saturday', 'Sunday']
        day_offset = 0    
        for i in range(7):
            if firstDay == days_in_week[i]:
                day_offset += i
        index_days_in_week = (currentDate + day_offset)%7 - 1
        return days_in_week[index_days_in_week]
        def main():
            firstDay = input("supply the first day of the month")
            currentDate = input("supply current day of the month")
            returnDay(firstDay, currentDate)

    You may need to omtimise it in order to work completely. I leave this as a homework assessment to you. I hope this helps clarifying your problems.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum