vendredi 4 septembre 2020

Python date generation not generating proper dates

I'm a beginner at python and I made this random date generator which should generate year-month-date output. And 4,6,9,11 months have 30 days, all others 31.

But I'm having a problem where february in leap year still generates date=30 despite if and elif having the condition where M must be 2.

import random
import calendar
for i in range(500):
    Y = rand.randint(0, 170)
    M = rand.randint(1, 12)
    
    if calendar.isleap(G+1850) == True and M == 2:
        D = rand.randint(1, 28)
    elif calendar.isleap(G+1850) == False and M == 2:
            D=rand.randint(1, 29)
    if M == 4 or M == 6 or M == 9 or M == 11:
            D=rand.randint(1, 30)
    else:
            D=rand.randint(1, 31)



Aucun commentaire:

Enregistrer un commentaire