-1

I am very new to Python and we were told to write the weekday function without any modules like e.g. daytime etc. But it doesn't work and i am not sure where is a problem

def weekDay (day,month,year):
    global y0
    global m0
    global x
    y0 = 0
    m0 = 0
    day,month,year = int(input("Enter a day: "))
    month = int(input("Enter a month: "))
    year = int(input("Enter a year:"))
    a = (day + x + (31 * m0) // 12) % 7
       for m0 in a:
          m0 = month + 12 * ((14 - month) // 12) - 2
       for x in a:
          x = y0 + y0 // 4 - y0 // 100 + y0 // 400
          for y0 in x:
             y0 = year - ((14 - month) // 12)
    if a == 0:
        print("Sunday")
    elif a == 1:
        print("Monday")
    elif a == 2:
        print("Tuesday")
    elif a == 3:
        print("Wednesday")
    elif a == 4:
        print("Thursday")
    elif a == 5:
        print("Friday")
    else:
        print("Error")

    return weekDay(a)

'''
here is the formula we were given:
[![formula][1]][1]


  [1]: https://i.stack.imgur.com/iBv30.png
123
  • 67
  • 5

2 Answers2

-1

This should help:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime('%A')
'Friday'
>>> 
lenik
  • 23,228
  • 4
  • 34
  • 43
  • This answer doesn't really help the post. He said he couldn't use any modules, and your solution is just for the today case, and not for a generic date. – João Castilho May 15 '20 at 11:49
  • @JoãoCastilho do you have any problem using `datetime.datetime(2020,5,15).strftime('%A')` that prints `Friday` ? or any other "generic" date for that matter? – lenik May 15 '20 at 12:03
  • @JoãoCastilho at least, now he/she has something to compare the results with... – lenik May 15 '20 at 12:12
-1

Global variables not defined anywhere and I am not able to understand the logic you are trying to write. So written a function based on a aptitude trick.

def weekday(day,month,year):
    """
    This function is written based upon aptitude trick 
    to obtain day from given a date.
    Input date example : 15-5-2020
    Link for logic : https://www.youtube.com/watch?v=rJ0_GWDTdD4
    """
    # for different months we are assigning specific number to that month
    months = {1:1, 2:4, 3:4, 4:0, 5:2, 6:5, 7:0, 8:3, 9:6, 10:1, 11:4, 12:6}

    # assigning days to a number
    day_name = {1:'Sunday', 2:'Monday', 3:'Tuesday', 4:'Wednesday', 5:'Thursday',
                6:'Friday', 0:'Saturday'}

    # to get the year in between 1600 and 2000. since we are assiging values
    # for the years also
    while year not in range(1600,2000):
        if year>2000:
            year-=400
        if year<1600:
            year+=400
    # assigning values to years
    if year in range(1600,1700):
        yr = 6 
    if year in range(1700,1800):
        yr = 4
    if year in range(1800,1900):
        yr = 2
    if year in range(1900,2000):
        yr = 0

    # assigning last two numbers of year to last
    first = year//100
    last = year - (first * 100)

    # obtaining remainder 
    res = (day + months[month] + yr + last + (last//4))%7

    #returning the day_name
    return day_name[res]

day,month,year = list(map(int,input("Enter date in format dd-mm-yyyy : ").split('-')))
print(weekday(day,month,year))

Hope, you are satisfied with logic.