0

I have written following Python Function which should return Day, when year, month & date is provided to function. For Testing, I have limit it to 3 years & then call the function using print. But it always return “Saturday”. What’s wrong in function.

enter code here
def dayOfYear(y,m,d):
    if y in [2018,2019,2020]:
        if m in [1,2,3]:
            if d in [31,28,1]:
                return('Saturday')
            else:
                return('Thursday')
        else:
            return('Sunday')
    else:
        return(None)

print(dayOfYear(2018,1,31))
print(dayOfYear(2019,2,28))
print(dayOfYear(2020,3,1))
Shahzad
  • 1
  • 2
  • 1
    Welcome to stack overflow. Reading https://stackoverflow.com/help/minimal-reproducible-example will give you a good good guide on how to ask questions. Also https://stackoverflow.com/help/how-to-ask will show what the comunity considers a good question. Its a great start but nowere in this code is Saturday... – Paul Brennan Dec 10 '20 at 17:28
  • Your program is working as expected. All test cases result in 'Saturday'. – 001 Dec 10 '20 at 17:35
  • Hi @JohnnyMopp It should return Saturday, Thursday & Sunday instead of just Saturday – Shahzad Dec 10 '20 at 17:39
  • For all 3 test cases, all 3 `if` statements are true so 'Saturday' is returned. To get a different result change your test cases. For example: `2018,1,31` and `2019,2,2` and `2020,4,1` – 001 Dec 10 '20 at 17:42
  • @JohnnyMopp After Changing the cases as per your direction i.e. print(dayOfYear2(2018,1,31)) print(dayOfYear2(2019,2,2)) print(dayOfYear2(2020,4,1)) Still getting Same result i.e. Saturday. I think IF statement needs some Fixing – Shahzad Dec 10 '20 at 17:57
  • @Shahzad https://ideone.com/GFWNMM – 001 Dec 10 '20 at 18:07
  • @JohnnyMopp That one is Better. But I want is that it should return me the Day against my given Year, Month & Date, not any other value. So even with yor provided solution, if I call function with my given year, month & Date, it still return Saturday. – Shahzad Dec 10 '20 at 18:38
  • I have got expected result with 3 separate IF statements but Nested IF failed – Shahzad Dec 10 '20 at 18:40
  • @JohnnyMopp https://ideone.com/PmRjhH Check this Solution. But Nested IF failed. Is there any way to fix it – Shahzad Dec 11 '20 at 10:19

0 Answers0