diff --git a/projects/Calculate_age/calculate.py b/projects/Calculate_age/calculate.py index cdbeb7044..4df52afff 100644 --- a/projects/Calculate_age/calculate.py +++ b/projects/Calculate_age/calculate.py @@ -1,4 +1,50 @@ # -*- coding: utf-8 -*- +# import time +# from calendar import isleap + +# # judge the leap year +# def judge_leap_year(year): +# if isleap(year): +# return True +# else: +# return False + + +# # returns the number of days in each month +# def month_days(month, leap_year): +# if month in [1, 3, 5, 7, 8, 10, 12]: +# return 31 +# elif month in [4, 6, 9, 11]: +# return 30 +# elif month == 2 and leap_year: +# return 29 +# elif month == 2 and (not leap_year): +# return 28 + + +# name = input("input your name: ") +# age = input("input your age: ") +# localtime = time.localtime(time.time()) + +# year = int(age) +# month = year * 12 + localtime.tm_mon +# day = 0 + +# begin_year = int(localtime.tm_year) - year +# end_year = begin_year + year + +# # calculate the days +# for y in range(begin_year, end_year): +# if (judge_leap_year(y)): +# day = day + 366 +# else: +# day = day + 365 + +# leap_year = judge_leap_year(localtime.tm_year) +# for m in range(1, localtime.tm_mon): +# day = day + month_days(m, leap_year) + +# day = day + localtime.tm_mday import time from calendar import isleap @@ -9,7 +55,6 @@ def judge_leap_year(year): else: return False - # returns the number of days in each month def month_days(month, leap_year): if month in [1, 3, 5, 7, 8, 10, 12]: @@ -21,9 +66,10 @@ def month_days(month, leap_year): elif month == 2 and (not leap_year): return 28 +# Use raw_input instead of input in Python 2 +name = raw_input("input your name: ") +age = raw_input("input your age: ") -name = input("input your name: ") -age = input("input your age: ") localtime = time.localtime(time.time()) year = int(age) @@ -35,15 +81,15 @@ def month_days(month, leap_year): # calculate the days for y in range(begin_year, end_year): - if (judge_leap_year(y)): - day = day + 366 + if judge_leap_year(y): + day += 366 else: - day = day + 365 + day += 365 leap_year = judge_leap_year(localtime.tm_year) for m in range(1, localtime.tm_mon): - day = day + month_days(m, leap_year) + day += month_days(m, leap_year) -day = day + localtime.tm_mday -print("%s's age is %d years or " % (name, year), end="") +day += localtime.tm_mday +print ("%s's age is %d years or " % (name, year)) print("%d months or %d days" % (month, day))