'How does the python interpreter know that only specific variables in the equation are to be multiplied by the number of days?
I am working on practice examples on edabit using python. One of the examples I am stuck on is as follows:
Create a function that takes the number of daily average recovered cases recovers, daily average new_cases, current active_cases, and returns the number of days it will take to reach zero cases.
def end_corona(recovers, new_cases, active_cases):
end_corona(4000, 2000, 77000) ➞ 39
end_corona(3000, 2000, 50699) ➞ 51
end_corona(30000, 25000, 390205) ➞ 79
Below is one of the correct solutions that I am not quite sure how the interpreter knows that only "new_cases" and "recovers" are supposed to be multiplied by the number of days (d). Somehow this solution does work but can someone explain to me why and how?
def end_corona(recovers, new_cases, active_cases):
days = 0
while active_cases > 0:
days += 1
active_cases = active_cases-recovers+new_cases
return days
Solution 1:[1]
The function is running in a loop. So in each iteration it subtracts the daily average recovered cases from the active cases and adds the daily average new cases and increase the number of days by one. It runs until the active cases are not zero and returns the total number of days it would take to get to zero cases.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniyal Ishfaq |
