Skip to content Skip to sidebar Skip to footer

Python Date Function Bugs

I am trying to create a function in python which will display the date. So I can see the program run, I have set one day to five seconds, so every five seconds it will become the n

Solution 1:

There's no possible path through your code where day gets incremented.

I think you are actually confused between > and <: you check if day is greater than 31 or 28, which it never is. I think you mean if day < 31: and so on.

Solution 2:

First of all, it's easier to just set time.sleep(5) instead of looping over time.sleep(1) 5 times. It's better to have a list of values with days of the month, not just 2 lists of the long and short months. Also your while loop is currently indefinite, is that intentional?

Anyway, your main problem was comparing day > 31, but there's lots of things that can be improved. As I said, I'm removing the use of oneDay to just do sleep(5) as it's cleaner and having one daysInMonths list.

import time

    def showDate():
        year=00month=1day=1
        daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Now you can have only one if check about if the day has reached the end of a month, like this:

        while True:
            time.sleep(5)
            if day< daysInMonths[month-1]:
                day+=1

This will check the index of the list for the current month. It uses -1 because lists begin at index 0, and your months begin at 1. (ie. the months run from 1-12 but the list's indices are 0-11). Also I used the += operator, which is basically short hand for var = var + something. It works the same and looks neater.

This test encompasses all months, and then the alternative scenario is that you need to increment the month. I recommend in this block that you first check if the month is 12 and then increment the year from there. Also you should be setting day and month back to 1, since that was their starting value. If it's not the end of the year, increment the month and set day back to 1.

else:
                if month==12:
                    year+=1day=1month=1else:
                    month+=1day=1
            print("{}/{}/{}".format(day, month, year))

I also used the string.format syntax for neatness. With format, it will substitute the variables you pass in for {} in the string. It makes it easier to lay out how the string should actually look, and it converts the variables to string format implicitly.

Solution 3:

Try this.

The day comparisons should be <, not >. When going to the next month, I set the day to 1, because there are no days 0 in the calendar. And I use elif for the subsequent month tests, because all the cases are exclusive.

def showDate():
    year=00month=1day=1
    oneDay =5
    longMonths = [1, 3, 5, 7, 8, 10, 12]
    shortMonths = [4, 6, 9, 11]
    while True:
        time.sleep(1)
        oneDay = oneDay -1
        if oneDay ==0:
            if monthin longMonths:
                if day<31:
                    day=day+1else:
                    month=month+1day=1
            elif month==2:
                if day<28:
                    day=day+1else:
                    month=month+1day=1
            if monthin shortMonths:
                if day<30:
                    day=day+1else:
                    month=month+1day=1
            if day==31andmonth==12:
                year=year+1month=1
            print(str(day) +'/'+ str(month) +'/'+ str(year))
            oneDay =5

Post a Comment for "Python Date Function Bugs"