Method to return string from an integer value

Asked

Viewed 74 times

-2

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


I tried that :

def meses_dias(dias):
    return("{} meses(s) e {} dias(s).".format(dias//7))
  • 1

    Could you improve the title of the question, right? The purpose of the title is to summarize the context of the question, but yours does not. Besides, in the code you posted, your string expects two figures: the amount of weeks (which is like months? ) and the number of days over (which has not been calculated).

  • I am new to the site friend and do not know use it yet .

  • Write a function called meses_days that gets an argument, an integer day, and return a string that says how many weeks and days that number represents. For example, months(10) should return, "1 week(s) and 3 days(s)."

  • the problem is exactly what I typed up.

  • 1

    @Cleomirsantos welcome to the site. Take the tour and learn how to improve your question (edit, format, etc): https://answall.com/tour

1 answer

3


The main problem in 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, that comes to complete a week. You can do this with the entire division and 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 on Repl.it

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

  • got it, I really got it confused

Browser other questions tagged

You are not signed in. Login or sign up in order to post.