Python date function bugs

2024/7/7 6:45:07

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 timedef showDate():year = 00month = 1day = 1oneDay = 5longMonths = [1, 3, 5, 7, 8, 10, 12]shortMonths = [4, 6, 9, 11]while True:time.sleep(1)oneDay = oneDay - 1if oneDay == 0:if month in longMonths:if day > 31:day = day + 1else:month = month + 1day = 0if month == 2:if day > 28:day = day + 1else:month = month + 1day = 0if month in shortMonths:if day > 30:day = day + 1else:month = month + 1day = 0if day == 31 and month == 12:year = year + 1print(str(day) + '/' + str(month) + '/' + str(year))oneDay = 5showDate()

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?

Answer

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.

https://en.xdnf.cn/q/120365.html

Related Q&A

Retreiving data from a website [duplicate]

This question already has answers here:How to determine the IP address of the server after connecting with urllib2?(4 answers)Closed 9 years ago.Im terribly sorry if this is unacceptable or answered e…

How to comma separate an array of integers in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 2.7.5 - Where is it installed on Windows Vista? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…

Python list of lists specific path combinations or permutations

I have a list of lists and am looking for something similar to combinations or permutations, but there are conditions that may result in good "Path" or "Dead_End". If "Dead_En…

Python packages.import sys vs from sys import argv

Im trying to use argv into my python script. For this im using following code:from sys import argv script, file_name = argv print(file_name)python3 e.py e.txt This code works fine.But when I use:import…

How to reorganize a list of tuples?

Say I had a list of tuples:[(98, studentA), (97, studentB), (98, studentC), (95,studentD)]And I wanted to organize it so that the students are grouped together by the first number in the tuple, what wo…

How to loop through json data with multiple objects

My json file data.json looks like this [ {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"}, {"host"…

python django only the first statement statement can be accessed

i can acccess only the first statement in my name appjavascript:<script type="text/javascript">function searched(){{% for names in name %}nameSearched = document.getElementById(name).va…

syntax error return outside function in python

I am trying to count the word fizz using python. However it is giving me an error.def fizz_count(x):count =0 for item in x :if item== "fizz":count=count+1 return countitem= ["fizz",…

How to replace values in multidimensional array?

I am trying to get a multidimensional array working, where the user string is filled in the cell. I have been searching for ways to update user values in the multidimensional array def createMultiArr…