2025年8月4日 星期一

Python Unit 3 Lesson 4: Loop control flow 的 Challenge: Infectious diseases

 import virus


population = 10000

infected = 1

can_catch_virus = population - infected

contacts_per_day = 10

newly_recovered = 0


for day in range(90): # day 從 0 開始,到 89。如果要從 1 開始到 90,就要寫成 range(1,91)。

    # Infected chickens spread the virus to those who haven't already had it.

    if day > 13: # 因為 day 是從 0 起算,所以 13 代表 day 14。

        contacts_per_day = 3

    newly_infected = virus.spread(

        infected, can_catch_virus, population, contacts_per_day

    )


    newly_recovered = virus.recover(infected)

    infected = infected + newly_infected - newly_recovered

    can_catch_virus = can_catch_virus - newly_infected


    print(str(infected) + " chickens infected.")


    if infected == 0 :

        break


print("----------")

print(str(population - can_catch_virus) + " chickens caught the virus.")


沒有留言:

張貼留言