John has decided to lose weight as a part of his New Year’s resolution. John works out every weekday. However, on weekends he can’t resist his cravings for fast food and binges at the local burger place. John loses 10 ounces for every work-out session on weekdays, but then gains 8 ounces each on Saturday and Sunday. John returns to his resolution on Monday and hits the gym. Can you help John in calculating the number of days required to reach his goal? Write the function days_for_target(), which accepts the following arguments: • initial_weight: a positive integer that represents John’s weight in ounces at the beginning • target_weight: a positive integer that represents John’s target weight in ounces We will assume that John starts working on his New Year’s resolution on a Monday, and that his target weight is less than his current weight. If John loses a little more weight than desired, then this is acceptable. For example, if the current day is a weekday, John currently weighs 2901 and has a target of 2900, then he would work out once more to reach 2801. The function will return 2801. Hint: treat Monday as day 0, Tuesday as day 1, ..., and Sunday as day 6. This pattern repeats every week, so the remainder operator can help you decide whether the current day is a weekday or a weekend day.

Respuesta :

Answer:

day variable is the day number of the week which takes value 0 to 6, and then again 0 to 6 and so on.

weight variable contains the current weight of John , initialised to the intitial_weight provided.

days variable contains the number of days John went to gym.

While loop is executed until the weight of John becomes less than or equal to the target_weight variable.

Code:

def days_for_target(initial_weight, target_weight):

day = 0

weight = initial_weight

days = 0

 

while(weight>target_weight):

if(day<5):

weight = weight - 10

day = day+1

elif(day==5):

weight = weight + 8

day = day+1

else:

weight = weight + 8

day = 0

 

days = days + 1

 

return days