#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")
CodePudding user response:
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
CodePudding user response:
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!";
}
}