最简单地说,养恤金是雇主根据雇员的工作时间向他们支付的金额。
下面的代码是基于输入数据的代码(“年度开始就业”、“月开始”、“日开始”)。
然后,我有一个循环,每一年,并计算你工作的天数,这一年之间的两个日期。
Let us say for this EXAMPLE:
-The Date Started: January 2, 2020
-The Date Ended: June 23,2023 from datetime import timedelta, date
#For the Input Information
#Beginning Dates
print("===============================START WORK DATE INFO======================================\n")
YearStartEMP = int(input("Please Enter The Year Employment Started:"))
MonthStartEMP = int(input("Please Enter The Month Employment Started:"))
StartDayEmp = int(input("Please Enter The Day # Employment Started:"))
#End of Work dates
print("===============================END WORK DATE INFO======================================\n")
YearEndEMP = int(input("Please Enter The Year Employment Ended:"))
MonthEndEMP = int(input("Please Enter The Month Employment Ended:"))
EndDayEmp = int(input("Please Enter The Day# Employment Ended:"))
print("===============================DAYS PER YEAR WORKED=======================================\n")
#dates entered
StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp) + timedelta(days=1)
#While loop to look through each year and count the days per year for each year betwwen dates
while StartWork < EndWork:
next_year = StartWork.replace(year=StartWork.year + 1, month=1, day=1)
if next_year < EndWork:
diff = next_year - StartWork
else:
diff = EndWork - StartWork
print(f'{StartWork.year}: {diff.days} days')
StartWork = next_year这就是我给出的信息的输出结果。
===============================START WORK DATE INFO=======================================
Please Enter The Year Employment Started:2020
Please Enter The Month Employment Started:1
Please Enter The Day # Employment Started:2
===============================END WORK DATE INFO=========================================
Please Enter The Year Employment Ended:2023
Please Enter The Month Employment Ended:6
Please Enter The Day# Employment Ended:23
===============================DAYS PER YEAR WORKED=======================================
2020: 365 days
2021: 365 days
2022: 365 days
2023: 174 days我的问题是,对于"===DAYS年WORKED===“下的代码部分,应该使用什么代码或函数来使输出看起来像这样。假设退休人员只需要180天就能获得年度的信用,我应该使用什么样的if语句或函数。应该是这样的。
2020: 365 days, 1 credit
2021: 365 days, 1 Credit
2022: 365 days, 1 Credit
2023: 174 days, 0 Credit 或者类似的东西:
2020: 365 days
2021: 365 days
2022: 365 days
2023: 174 days
2020: 1 credit
2021: 1 Credit
2022: 1 Credit
2023: 0 Credit 我尝试过这样做,但是它导致了一个错误:
if DateBracket >= 180:
Credit = 1
print("1 credit")
elif DateBracket < 180:
Credit = 0
print("0 credit")发布于 2022-09-11 08:01:06
这是你的工作代码。我添加了一些评论来解释我所做的改变:
from datetime import timedelta, date
# For the Input Information
# Beginning Dates
print("===============================START WORK DATE INFO======================================\n")
# Added the `.strip()` method to remove whitespaces from the input
YearStartEMP = int(input("Please Enter The Year Employment Started: ").strip())
MonthStartEMP = int(input("Please Enter The Month Employment Started: ").strip())
StartDayEmp = int(input("Please Enter The Day # Employment Started: ").strip())
# End of Work dates
print("===============================END WORK DATE INFO======================================\n")
YearEndEMP = int(input("Please Enter The Year Employment Ended: ").strip())
MonthEndEMP = int(input("Please Enter The Month Employment Ended: ").strip())
EndDayEmp = int(input("Please Enter The Day # Employment Ended: ").strip())
print("===============================DAYS PER YEAR WORKED=======================================\n")
# Dates entered
StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp) + timedelta(days=1)
# Also keep track of the total number of credits for simplicity
total_credits = 0
# While loop to look through each year and count the days per year for each year betwwen dates
while StartWork < EndWork:
next_year = StartWork.replace(year=StartWork.year + 1, month=1, day=1)
if next_year < EndWork:
# We only need the number of days worked in this year, not the whole timedelta
days_per_year = (next_year - StartWork).days
else:
days_per_year = (EndWork - StartWork).days
# Set `credits` to 1 if the employee worked at least 180 days in the year
# Else, set `credits` to 0
credits = 1 if days_per_year >= 180 else 0
# The code above is equivalent to the following, but it's cleaner
"""
if days_per_year >= 180:
credits = 1
else:
credits = 0
"""
# Also print the credits for the year
print(f'{StartWork.year}: {days_per_year} days, {credits} credit(s)')
StartWork = next_year
# Update the total number of credits
total_credits += credits
print(f'Total credits: {total_credits}')这是样本输出:
===============================START WORK DATE INFO======================================
Please Enter The Year Employment Started: 2020
Please Enter The Month Employment Started: 1
Please Enter The Day # Employment Started: 2
===============================END WORK DATE INFO======================================
Please Enter The Year Employment Ended: 2023
Please Enter The Month Employment Ended: 6
Please Enter The Day # Employment Ended: 23
===============================DAYS PER YEAR WORKED=======================================
2020: 365 days, 1 credit(s)
2021: 365 days, 1 credit(s)
2022: 365 days, 1 credit(s)
2023: 174 days, 0 credit(s)
Total credits: 3https://stackoverflow.com/questions/73674571
复制相似问题