我正在嘗試將日期轉換為整數以進行比較。
# Python3 code to calculate age in years
import datetime
from datetime import date
date_entry = input('Enter your birthdate in YYYY/MM/DD format: ')
year, month, day = map(int, date_entry.split('/'))
date1 = datetime.date(year, month, day)
def calculateAge(birthDate):
today = date.today()
age = today.year - birthDate.year - \
((today.month, today.day) < (birthDate.month, birthDate.day))
return age
# Driver code
print(calculateAge(date1), "years")
if date1 < 18:
print('You are under age')
exit()
我的 if 陳述句中有一個錯誤,因為 date1 不是整數。我該如何解決這個問題?
謝謝。
uj5u.com熱心網友回復:
你可以編輯如下。
if calculateAge(date1) < 18:
print('You are under age')
exit()
在此處輸入影像描述
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/448036.html
