Method to return string of an integer value

-2

I have a meses_dias function that receives an argument, an integer days, and returns a string that says how many weeks and days that number represents. For example, meses_dias(10) should return 1 semana(s) e 3 dias(s)

I've tried this:

def meses_dias(dias):
    return("{} meses(s) e {} dias(s).".format(dias//7))
    
asked by anonymous 18.04.2018 / 17:28

1 answer

2

The main problem with your code is that your string expects two values, but you are indicating only one. You need to calculate the number of weeks and the surplus of days, which goes to complete a week. You can do this with the whole division and with the rest of the division, or alternatively with the function divmod :

def meses_dias(dias):
    semanas, dias = divmod(dias, 7)
    return f"{semanas} semana(s) e {dias} dias(s)."

print(meses_dias(10)) # 1 semana(s) e 3 dias(s).

See working at Repl.it

  

Note: meses_dias is a bad name for a function that calculates the number of weeks.

    
18.04.2018 / 18:05