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.")


教師留言給晉維英文課-8月4日

     2025/08/04(一) 8:00-8:50 

  1. 今日教學進度:
    1. 教師和晉維檢討上週作業指派。晉維有複習聽力練習Marathons,和《Heart of the Matter》第 30 段語意解說(Semantic Analysis)之 Conclusion  及第 31 段 1-3 句,《弦元佑文法解題》第10章語態。
    2. 教師帶領晉維精讀《Heart of the Matter》第 32 段 1-3 句、第 32 段之文意解說之第 1-2 節。
  2. 作業指派:
    1. 請晉維自自修聽力練習Climate change - can we stop it? 》。
    2. 請晉維複習《Heart of the Matter》第 32 段 1-3 句、第 32 段之文意解說之第 1-2 節。
    3. 請晉維預習《Heart of the Matter》第 32 段之文意解說之第 3 節,Semantic Themes and Relationships , Implications and Interpretations,Conclusion,和 Overall Message。
    4. 請晉維自修《弦元佑文法解題》第13章動名詞。

  3. 下次課程計畫: 
    1. 檢閱作業派執行狀況
    2. 教師指導晉維精讀 《Heart of the Matter》第 32 段之文意解說之第 3 節,Semantic Themes and Relationships , Implications and Interpretations,Conclusion,和 Overall Message。
  4. 附註:
    1. 教師將請假赴美,期間: 8/18(一)~9/3(三)。有三個星期一:8/18、8/25、和 9/1。
    2. 在此期間教師將指派作業給晉維自修。
      1. 關於聽力練習部分:1) Where money has no value 2) Scouting moves ahead
      2. 關於弦元佑文法解題部份:1) 第14章不定詞片語,2) 第15章對等連接詞。
    3. 下次上課日期預訂9/8(一),可能會需要配合晉維學校的課表而變動。

教師留言給尤靖恩尤佳恩程式設計課-8/4

    2025/08/04(一) 14:00-16:20

  1. 今日教學進度:
    1. 教師帶領靖恩和佳恩檢討上次作業指派:
      1. 猜數字遊戲 (使用 random)
      2.  • 程式隨機選數字,程式自猜,程式提示太大或太小。
      3.  • 強化點:
      4.  • 用 random.randint()。
      5.  • 加入計分機制與猜錯次數。
      6.  • 顯示「你用了幾次才猜對」。
      7. 使用 Thonny 平台。
      8. 靖恩完成程式設計,設定數字範圍為 1~1,000,測試約 30 次,結果都在 10 次以下猜對。
      9. 這符合每次猜中間數的演算法的 BigO 預測:log(1000,2) =  9.966。
    2. 教師帶領靖恩和佳恩學習 Python Unit 3 Lesson 4: Loop control flow 的 Practice: Trace break and continue statements,完成 3 題,共 4 題。最後一題題目如下,待下次完成:

  2. 作業指派:
    1. 請靖恩和佳恩複習 Khan Academy Python Unit 3 Lesson 4: Loop control flow 的教學影片、文件、和測驗。
    2. 請靖恩和佳恩自修 Khan Academy Python Unit 3 Lesson 4: Loop control flow 的 Challenge: Infectious diseases。當看不懂程式說明的英文時,請複製到 Google 翻譯,可以看中文。也可以用 Yahoo 字典查音標、聽語音、以及單字之詞類及用法。
  3. 下次課程計畫:
    1. 2025/08/11(一) 14:00-16:00
    2. 教師檢查靖恩及佳恩作業
    3. 教師指導靖恩及佳恩學習 Khan Academy Python Unit 3 Lesson 5: Simulating populations。

2025年8月2日 星期六

猜數字:程式隨機出題,程式自行解題

import random

import math

max = 1000 # 最大數字

number = random.randint(1,max) # 程式隨機出題, 1~1000 之間。

score = math.ceil(math.log(max,2)) # 起始分數為最大數字取2為底的對數的下一個整數,若猜中時,得到負分表示猜數的演算法不佳

got_it = False # False 表示還沒有答對,True 表示答對了

times = 0 # 計算共猜了多少次

# 演算法:用持續限縮範圍法,一直把可能的最小數字和最大數字加總除以 2 ,直到猜中為止。

low = 1 # 可能的最小數字

high = max # 可能的最大數字

print("Your initial score is " + str(score) +".")

while got_it == False: # 當還沒有猜對,就繼續猜

    times += 1 # 猜的次數加 1

    print("------------------") # 每次猜測相關數字的分隔線

    print("low = " + str(low)) # 現示最小可能數字

    print("high = " + str(high)) # 現示最大可能數字

    guess = int((low+high)/2) # 試猜數字 = 把可能的最小數字和最大數字加總除以 2 

    print("You guess " + str(guess) + ".")

    print("number = " + str(number))

    if guess > number: #  如果猜的數字太大

        score -= 1 # 扣一分

        high = guess-1 # 把最大可能數字改為試猜數字減 1

        print("Too big! Try again.") # 顯示所猜數字太大

        print("Your score is " + str(score) + ".") # 顯示目前分數

    elif guess < number: #  如果猜的數字太小

        score -= 1# 扣一分

        low = guess+1 # 把最小可能數字改為試猜數字加 1

        print("Too small! Try again.") # 顯示所猜數字太小

        print("Your score is " + str(score) + ".") # 顯示目前分數

    else: # 答對了

        got_it = True # 把 got_it 改為 True,表示答對了。原本是 False

        print("Congratulation! You got it!") # 顯示答對了

        print("Your have tried " + str(times) + " times.") # 顯示共猜了多少次

        print("Your score is " + str(score) + ".") # 顯示最後得分