0

#It prints the list of all the dates in the mm/dd/22 format. I can't figure out how to make an exception for the odd months having 30 days instead of 31. I've tried making a list outside the for loop for example:

n = list(range(1,31)

except it turns into loop hell and basically prints infinitely

Also I can't make an exception for the 28 days in February. Any further for loops within the already nested for loop prints nested 2/##/22.

for i in range(1,13):
    for j in range(1,32):
        if (i%2) == 0 and i !=2:
         #test works
         #figure out how to limit the days {j} from 31 to 30
            print(f"{i}/{j}/22")
        elif i == 2:
        #figure out how to limit the dates {j} to 28
            print(f"{i}/{j}/22")
        else:
            print(f"{i}/{j}/22")
  • 1
    You can use this formula `year%4==0 && year%100!=0 || year%400==0` to check if the given year is leap year or not! Based on the output of this formula you can decide if the February of 2022 has 28 or 29 days! – Vishnu Oct 10 '21 at 23:38
  • As @Vishnu said above, using the % operator, also known as modulus operator as a condition for the leap year, you can make your exception. – Austin Oct 10 '21 at 23:39
  • *"the odd months having 30 days instead of 31"* -- That's incorrect. They switch at August. – wjandrea Oct 10 '21 at 23:48
  • 1
    Scroll through the answers here: https://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python (just update the format to `%d/%m/%y`) – Thomas Oct 10 '21 at 23:52

2 Answers2

2

Don't reinvent the wheel, use the datetime module instead.

from datetime import date, timedelta

year = 2022
start = date(year, 1, 1)
end = date(year+1, 1, 1)
delta = end - start
for offset in range(delta.days):
    day = start + timedelta(offset)
    print(day.strftime('%d/%m/%y'))

Sample output:

01/01/22
02/01/22
03/01/22
...
28/02/22
01/03/22
...
31/07/22
...
31/08/22
...
31/12/22
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Thank you so much. The purpose of this was just to work on my nested loop logic. After 3 hours I don't really see the point anymore. You're right, why reinvent the wheel. – Koby Noguchi Oct 10 '21 at 23:54
  • @Koby Exactly, Python has [batteries included](https://www.python.org/dev/peps/pep-0206/#batteries-included-philosophy), so you don't need to implement everything yourself. That said, implementing stuff yourself is good for practice! It makes you think about edge cases like leap years :) – wjandrea Oct 11 '21 at 00:08
0

This is a basic code which can check if the given year in leap or not in C++, I hope you can port the logic to Python to get your answer! Use the formula of leap year if you want to use loops OR else use the datetime library of python!

#include <iostream>

using namespace std;

int main()
{
    //Rule for checking leap years is:
    //(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) -> definition for a leap year i.e., year that has 366 days OR a february in this year which has 29 days

    int year, month;
    cout << "Year, month: ";
    cin >> year >> month;

    switch (month)
    {
        case 2:(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? cout << "29 days month!" : cout << "28 days month!"; break;

        case 4:
        case 6:
        case 9:
        case 11: cout << "30 days month!"; break;

        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: cout << "31 days month!"; break;
        default: cout << "Not valid!";
    }

}
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vishnu
  • 495
  • 8
  • 14