Finding the date of the next day-python
I need to find the date of the next day but without the datetime module
For example, First I have a function that take a date in parameter and I need to validate the date format via a input if the date format is right I display the date of the next day if the day format is wrong I simply display a message "it's not the good date format"
def tomorrow(date : str) -> str:
format='AAAA-M-J'
if begin == 'YES':
year = int(input('Chose a year:'))
month = int(input('Chose a month:'))
day = int(input('Chose a day:'))
if month == 12 and day == 31:
year = year=+1
month = month=+1
day = day=+1
date = str(f'{year}-{month}-{day}')
if len(date) == len(format):
return print('next date:' + date)
elif day == 30 and month%2 != 0 or day == 31 and month%2 == 0:
month = month=+1
day = day=+1
date = str(f'{year}-{month}-{day}')
if len(date) == len(format):
return print('next date:' + date)
elif day < 30 or 31:
day = day=+1
date = str(f'{year}-{month}-{day}')
if len(date) == len(format):
return print('next date:' + date)
else:
return print('Not a good date format')
else:
return print('NO')
date = print('date')
begin = str(input('type YES or NO'))
print(tomorrow(date))
nothing is displayed at the end
What's wrong ?
Solution 1:
- Your function doesn't seem to need the
date
parameter, it is reassigned inside the function - The
print
function returnsNone
, so your functiontomorrow
will always return None. -
year = year=+1
is equivalent toyear = 1
, you need to determine if this is what you want. - There is a problem with the way to determine whether the time string is in the correct format, such as
len("2021-10-21") != len("AAAA-M-J")
.
The above is the existing problem, I have made the modification below.
Is the format of the time string using regular matching(re
) correct, and simplify part of the code.
\d
For Unicode (str) patterns: Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched.{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match 'aaaab' or a thousand 'a' characters followed by a 'b', but not 'aaab'. The comma may not be omitted or the modifier would be confused with the previously described form.
\d{4}-\d{1,2}-\d{1,2}
This regular expression means a string that matches four numbers and one or two numbers one or two numbers, and is concatenated using
-
. For example:2021-1-1
,2021-12-29
.
import re
date_rule = re.compile(r"\d{4}-\d{1,2}-\d{1,2}")
def tomorrow(begin) -> str:
if begin == 'YES':
year = int(input('Chose a year:'))
month = int(input('Chose a month:'))
day = int(input('Chose a day:'))
# Determine if it is the last day of the year.
if month == 12 and day == 31:
year += 1
month = 1
day = 1
# Determine if it is the last day of the month.
elif day == 30 and month % 2 != 0 or day == 31 and month % 2 == 0:
month += 1
day = 1
# In other cases, that is, it is not the last day of the year or month, the number of days is incremented by 1.
else:
day += 1
date = f'{year}-{month}-{day}'
# If it can match successfully, it means that the format is correct.
if date_rule.match(date):
return 'next date:' + date
else:
return 'Not a good date format'
else:
return 'NO'
print('date')
begin = input('type YES or NO')
print(tomorrow(begin))