Python - 输入用户输入的天数,然后显示等效的年、月和日

标签 python calculator

我正在努力寻找针对日期部分的算法。不过,我可以把几年和几个月记下来。这是我的代码

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/

相关文章:

python - 如何在 python 中重复一个操作以使我的代码更简单

cuda - 了解入住率计算器

python - Odoo 更改宽度弹出窗口

python - odoo many2one 作为选择字段

python - 为什么 "test".count ('' ) 返回 5?

python - OpenCV将像素列表从一个图像复制到另一个图像

c# - 在 Windows 10 中将计算器窗口置于前面

objective-c - 在计算器应用程序中更改科学记数法的格式

ios - 计算器小数点

c- 逆波兰表示法计算器错误