我真的被我的代碼的這一部分困住了,從其他人那里得到一些輸入會很棒。這部分不是我寫的,我無法聯系到寫的人(這是一個小組專案)。當您沒有正確格式化輸入時,它確實會出現錯誤(我們不需要為此專案進行驗證,所以我不會擔心),但我擔心的是這些錯誤:
Traceback (most recent call last):
File "/Users/mycomputer/PycharmProjects/project/main.py", line 46, in <module>
stringsdates()
File "/Users/mycomputer/PycharmProjects/project/main.py", line 29, in stringsdates
birthmonth = birthday.month
AttributeError: 'str' object has no attribute 'month'
非常感謝任何幫助<3 我真的不知道在這部分代碼中從哪里開始。順便說一下,這里是代碼:
import datetime
def yearadd(date):
test = datetime.timedelta(days=365)
return date test
def stringsdates():
# Inputs for first and last name, start date and birthday along with yearly salary.
firstname = input("What is your first name? ")
lastname = input("What is your last name? ")
startdate = input("What is the date you started with the company? (YYYY MM DD) ")
birthday = input("What is your birthday? (YYYY MM DD) ")
yearlysal = input("What is your yearly salary? ")
# ? Processing
startdate = datetime.datetime.strptime(startdate, "%Y %m %d")
birthdate = datetime.datetime.strptime(birthday, "%Y %m %d")
yearhired = startdate.year
birthmonth = birthday.month
today = datetime.datetime.today()
formatfullname = (firstname lastname ", " firstname[0] "." lastname ", " lastname ", " firstname[
0] ".")
employeenum = firstname.upper()[0] lastname.upper()[0] "-" str(yearhired) "-" str(birthmonth)
reviewdate = yearadd(yearhired)
nextbday = today - birthdate
# ? Output
print(formatfullname)
print(employeenum)
print(reviewdate)
print(nextbday)
print(yearlysal)
uj5u.com熱心網友回復:
您正在嘗試讀取birthmonth,birthday.month因為birthday 是您從用戶那里讀取的字串,并且由于字串型別沒有與之關聯的月份屬性,您會收到錯誤訊息。您可能打算使用的是轉換后的日期時間物件birthdate。
birthmonth = birthdate.month
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/365479.html
