samedi 12 septembre 2020

How to make seperate if statements in python

I'm trying to write a program that takes an integer, seconds, as an input and returns it as, n years, n days, n hours, n minutes and n seconds.(n could be different for each)

But for some reason it doesn't go in the second if statement. I've put print("x") to test if it goes in the if statement, but the program only prints out one "x", which is in the first if statement, and it doesn't go in the other ones.

I've been trying to figure out why it does that, but I can't seem to do it. I've also looked up some stuff on the internet, so that I wouldn't bother someone with a question that has a reasonably easy answer, but I couldn't find anything.

Here is my code:

def format_duration(seconds):
    decimal_list = []

    second_decimal = seconds % 60
    seconds -= second_decimal
    seconds /= 60

    decimal_list.append(int(second_decimal))

    if seconds >= 60:
        minute_decimal = seconds % 60
        seconds -= minute_decimal
        seconds /= 60

        decimal_list.append(int(minute_decimal))
        print("x")

        if minute_decimal > 1:
            minute_txt = "minutes"
    
        else:
            minute_txt = "minute"

    if seconds >= 3600:
        hour_decimal = seconds % 24
        seconds -= hour_decimal
        seconds /= 24

        decimal_list.append(int(hour_decimal))
        print("x")

        if hour_decimal > 1:
            hour_txt = "hours"
            
        else:
            hour_txt = "hour"

    if seconds >= 86400:
        day_decimal = seconds % 365
        seconds -= day_decimal
        seconds /= 365

        decimal_list.append(int(day_decimal))
        print("x")

        if day_decimal > 1:
            day_txt = "days"
                
        else:
            day_txt = "day"

    if seconds >= 31536000:
        year_decimal = seconds

        decimal_list.append(int(year_decimal))
        print("x")

        if year_decimal > 1:
            year_txt = "years"
                    
        else:
            year_txt = "year"

    for i in range(len(decimal_list)):
        if decimal_list[i] == 0:
            decimal_list.remove(decimal_list[i])

    print(decimal_list)

format_duration(2354678)

Aucun commentaire:

Enregistrer un commentaire