Case 1
Enter the date: 5/7/2016
The incremented date is: 6 7 2016
Case 2
Enter the date: 30/2/1997
Date is invalid.
Case 3
Enter the date: 31/12/2016
The incremented date is: 1 1 2017
Bài 15: Viết chương trình Python để kiểm tra xem ngày có hợp lệ hay không và in ngày tăng dần nếu có
Example code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
date = input('Enter the date: ')
(dd, mm, yy) = date.split('/')
dd = int(dd)
mm = int(mm)
yy = int(yy)
if mm == 1 or mm == 3 or mm == 5 or mm == 7 or mm == 8 or mm == 10 \
or mm == 12:
max1 = 31
elif mm == 4 or mm == 6 or mm == 9 or mm == 11:
max1 = 30
elif yy % 4 == 0 and yy % 100 != 0 or yy % 400 == 0:
max1 = 29
else:
max1 = 28
if mm < 1 or mm > 12:
print 'Date is invalid.'
elif dd < 1 or dd > max1:
print 'Date is invalid.'
elif dd == max1 and mm != 12:
dd = 1
mm = mm + 1
print ('The incremented date is: ', dd, mm, yy)
elif dd == 31 and mm == 12:
dd = 1
mm = 1
yy = yy + 1
print ('The incremented date is: ', dd, mm, yy)
else:
dd = dd + 1
print ('The incremented date is: ', dd, mm, yy)