Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

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 next 'day' and it will print the date.

I know there is already an in-build function for displaying a date, however I am very new to python and I am trying to improve my skills (so excuse my poor coding.)

I have set the starting date to the first of January, 2000.

Here is my code:

import time

def showDate():
    year = 00
    month = 1
    day = 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 month in longMonths:
                if day > 31:
                    day = day + 1
                else:
                    month = month + 1
                    day = 0
            if month == 2:
                if day > 28:
                    day = day + 1
                else:
                    month = month + 1
                    day = 0
            if month in shortMonths:
                if day > 30:
                    day = day + 1
                else:
                    month = month + 1
                    day = 0
            if day == 31 and month == 12:
                year = year + 1
            print(str(day) + '/' + str(month) + '/' + str(year))
            oneDay = 5

showDate()

However, when I try to run the program this is the output I get this:

>>> 
0/3/0
0/5/0
0/7/0
0/8/0
0/10/0
0/12/0
0/13/0
0/13/0
0/13/0

I don't know why this is happening, could someone please suggest a solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...