【发布时间】:2022-01-03 08:20:36
【问题描述】:
我真的被我的这部分代码困住了,如果能从其他人那里得到一些输入,那就太棒了。这部分不是我写的,也无法联系到写的人(这是一个小组项目)。当您没有正确格式化输入时,它确实会出现错误(我们不需要对此项目进行验证,所以我不会担心),但我担心的是这些错误:
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'
非常感谢任何帮助
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)
【问题讨论】:
-
您访问的是
birthday.month,但也许您的意思是birthdate.month?birthday来自您的输入,因此它将是一个字符串
标签: python string datetime object attributeerror