我正在努力寻找针对日期部分的算法。不过,我可以把几年和几个月记下来。这是我的代码
def main():
# Prompt the user for an integer that represents a total number of days
user_days = int(input("Enter a total number of days: "))
# constant variables for: years, months, days
DAYS_IN_YEAR = 365
DAYS_IN_MONTH = 30
# Calculate the user's days into equivalent years
years = (int(user_days // DAYS_IN_YEAR))
# Calculate the user's days into equivalent months
months = (int(user_days // DAYS_IN_MONTH))
# Calculate the user's days into equivalent days
# days = (int( user_days - DAYS_IN_MONTH ))
# days = (int( ))
# give user their results
print(user_days, "days are equivalent to: ")
# display the equivalent years
print("Years: ", years)
# display the equivalent months
print("Months: ", months)
# display the equivalent days
print("Days: ", days)
main()
最佳答案
首先,你用你的天
做floor div
除以365,得到你的年。然后我们需要剩余的天数,因此我们使用天数模
365 并获得剩余的天数,我们将这些天数floor div
除以30,以获得我们的月份。然后我们用原来的剩余天数和 modulus
30 来得到剩余的天数
days = int(input())
years = days // 365
years_r = days % 365
months = years_r // 30
days_r = years_r % 30
400 Years: 1, Months: 1, Days: 5 500 Years: 1, Months: 4, Days: 15
关于Python - 输入用户输入的天数,然后显示等效的年、月和日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508876/