好了,所以,我嘗試讓我的代碼詢問您的出生日期,然後用這個數字確切(到秒),你的年齡 我的代碼的Python使if語句改變一個變量
import datetime
now = datetime.datetime.now()
day = int(input("What day was you born DD/MM/YYYY (DAY ONLY)"))
month = int(input("What month was you born? DD/MM/YYYY (MONTH ONLY)"))
year = int(input("What year was you born? DD/MM/YYYY (YEAR ONLY)"))
yearold = now.year - year
print("You are", yearold, "years old")
monthold = yearold * month
monthsold = now.month + monthold
print("You are", monthsold, "Months old")
問題是它會增加一年,所以它需要問你的生日是否過去了,如果它還沒有編輯yearold -1 的值,例如,生日10/10/2000 在事實上會說14其13
讓python爲你做計算,使用'datetime.timedelta()'http://docs.python.org/2.7/library/datetime.html#timedelta-objects –
這與你的問題無關,但是你的行monthold = yearold * month是錯誤的。如果你出生在1月(1月)和12歲,那會說他們已經12個月大了。我想你想monthold = yearold * 12,然後我想你想要monthsold = monthsold + now.month - month來實際計算它。 – DivineWolfwood