Try   HackMD

wk10_1109_hackMd_函式與模組

  1. hackMd 使用教學

    ​​​​​​​封面、首頁、目錄
    
    ​​​​​​​筆記內頁
    
  2. ch07_函式與模組

    ​​​​​​​7.1  自訂函式
    
    ​​​​​​​7.2  數值函式
    
    ​​​​​​​7.3 字串函式
    
    ​​​​​​​7.4 亂數模組
    
    ​​​​​​​7.5 時間模組
    

【inclass practice】

def sayHello() :
    print("hello, welcome !!!")
    
    
sayHello()
​​​​hello, welcome !!!
sayHello()
​​​​hello, welcome !!!
def getArea(w, h) :
    area = w * h
    return area

print(getArea(5, 10))

area1 = getArea(h = 10, w = 3)
print(area1)
​​​​50
​​​​30
def Circle(radius) :
    area = radius * radius * 3.14
    length = 2 * radius * 3.14
    return area, length

print(Circle(5))
print(Circle(7.7))
​​​​(78.5, 31.400000000000002)
​​​​(186.17060000000004, 48.356)
{範例}

攝氏溫度轉華氏溫度 <ctof>
學生分蘋果 <divmod>
總和及排序 <sorted>
檢查網址格式 <startswith>
以字串排版函式列印成績單 <just>
轉換日期格式 <replace>
擲骰子遊戲 <randint>
大樂透中獎號碼 <sample>
列印時間函式所有資訊 <localtime>
計算執行一百萬次整數運算的時間 <pcounter>

def cToF(x) :
    f = 1.8 * x +32
    return f

print(cToF(32))
​​​​89.6
{綜合演練}

實作3:

以十二小時(上午、下午)顯示現在時刻。

實作4:

小華媽媽上菜市場買菜,找錢時她希望在1元、5元、10元、50元硬幣中找回最少的硬幣。小華就利用自訂函式幫媽媽寫了一個程式。

{補充練習}

1A2B

函式1 : 數字相同+位置相同
函式2 : 數字相同 + 位置不同

#單字和定義的字典

word_dict = {
    "apple": "a round fruit with red or green skin and sweet flesh",
    "dog": "a domesticated mammal that is related to the wolves",
    "computer": "an electronic device for storing and processing data",
    "ocean": "a large body of saltwater that covers most of the Earth's surface",
    "book": "a written or printed work consisting of pages glued or sewn together along one side and bound in covers",
    "mountain": "a large natural elevation of the earth's surface rising abruptly from the surrounding level"
}

print("歡迎來到英文單字練習!")
score = 0
for word, definition in word_dict.items():
    print(f"定義: {definition}")
    user_input = input(f"請輸入單辭的拼寫: ").strip().lower()
    if user_input == word:
        print("正確!")
        score += 1
    else:
        print(f"錯誤。正確答案是 '{word}'。")

print(f"練習結束。您的得分是 {score}/{len(word_dict)}。")
​​​​歡迎來到英文單字練習!
​​​​定義: a round fruit with red or green skin and sweet flesh
​​​​請輸入單辭的拼寫: apple
​​​​正確!
​​​​定義: a domesticated mammal that is related to the wolves
​​​​請輸入單辭的拼寫: dog
​​​​正確!
​​​​定義: an electronic device for storing and processing data
​​​​請輸入單辭的拼寫: computer
​​​​正確!
​​​​定義: a large body of saltwater that covers most of the Earth's surface
​​​​請輸入單辭的拼寫: ocean
​​​​正確!
​​​​定義: a written or printed work consisting of pages glued or sewn together along one side and bound in covers
​​​​請輸入單辭的拼寫: book
​​​​正確!
​​​​定義: a large natural elevation of the earth's surface rising abruptly from the surrounding level
​​​​請輸入單辭的拼寫: mountain
​​​​正確!
​​​​練習結束。您的得分是 6/6。
#以下是一個簡單的猜字遊戲,使用字典作為遊戲中的單詞庫。
#玩家需要猜測英文單詞的意思,並獲得積分。程式會隨機選取一個單詞,玩家需要輸入其意思。

import random

# 單詞庫,包含英文單詞和它們的中文意思
word_dict = {
    "apple": "蘋果",
    "banana": "香蕉",
    "cat": "貓",
    "dog": "狗",
    "elephant": "大象",
    "flower": "花",
    "guitar": "吉他",
    # 添加更多單詞...
}

score = 0

# 遊戲主迴圈
while True:
    # 從單詞庫中隨機選擇一個單詞
    word, meaning = random.choice(list(word_dict.items()))

    print(f"請問 '{word}' 的中文意思是?")
    player_guess = input("請輸入中文意思: ")

    # 檢查答案是否正確
    if player_guess == meaning:
        print("正確!加10分。\n")
        score += 10
    else:
        print(f"答錯了,正確答案是 '{meaning}'。\n")

    # 顯示分數
    print(f"你的分數: {score} 分\n")

    # 問用戶是否繼續遊戲
    play_again = input("是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): ")
    if play_again.lower() == 'quit':
        break

print("遊戲結束,你的最終分數是:", score)
​​​​請問 'banana' 的中文意思是?
​​​​請輸入中文意思: 香蕉
​​​​正確!加10分。
​​​​
​​​​你的分數: 10 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'banana' 的中文意思是?
​​​​請輸入中文意思: 香蕉
​​​​正確!加10分。
​​​​
​​​​你的分數: 20 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'cat' 的中文意思是?
​​​​請輸入中文意思: 貓
​​​​正確!加10分。
​​​​
​​​​你的分數: 30 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'dog' 的中文意思是?
​​​​請輸入中文意思: 狗
​​​​正確!加10分。
​​​​
​​​​你的分數: 40 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'dog' 的中文意思是?
​​​​請輸入中文意思: 狗
​​​​正確!加10分。
​​​​
​​​​你的分數: 50 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'cat' 的中文意思是?
​​​​請輸入中文意思: 貓
​​​​正確!加10分。
​​​​
​​​​你的分數: 60 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'cat' 的中文意思是?
​​​​請輸入中文意思: 貓
​​​​正確!加10分。
​​​​
​​​​你的分數: 70 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'banana' 的中文意思是?
​​​​請輸入中文意思: 香蕉
​​​​正確!加10分。
​​​​
​​​​你的分數: 80 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'cat' 的中文意思是?
​​​​請輸入中文意思: 貓
​​​​正確!加10分。
​​​​
​​​​你的分數: 90 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): 
​​​​請問 'flower' 的中文意思是?
​​​​請輸入中文意思: 花
​​​​正確!加10分。
​​​​
​​​​你的分數: 100 分
​​​​
​​​​是否要繼續遊戲?(輸入 'quit' 退出遊戲,其他任意鍵繼續): quit
​​​​遊戲結束,你的最終分數是: 100

【afterclass practice】

  1. 綜合演練 選擇題1-10 (需抄題在markdown cell ; 有程式碼的題目要有code cell )
  2. 教學影音:
  • 新手入門 #07 Function (函式)
  • 新手入門 #09 Module (模組)

Ch07

  1. 函式的傳回值,下列何者正確?

    ​​​​​​​(A)無傳回值 (B) 1 個傳回值 (C) 2 個傳回值 (D)以上皆可
    
    ​​​​​​​Ans:( D ) 
    
  2. print(max([4,8,3,9,2,6])) 顯示的結果為何?

    ​​​​​​​(A)4 (B)6 (C)9 (D)2
    
    ​​​​​​​Ans:( C )
    
  3. print(pow(2,5,7)) 顯示的結果為何?

    ​​​​​​​(A)2 (B)4 (C)5 (D)7
    
    ​​​​​​​Ans:( B )
    
  4. print("hospital".replace("s","t")) 顯示的結果為何?

    ​​​​​​​(A)hotpital (B)hospisal (C)hospital (D)hotpisal
    
    ​​​​​​​Ans:( A )
    
  5. print("hospital".startswith("ho")) 顯示的結果為何?

    ​​​​​​​(A)True (B)False (C)hospital (D)ho
    
    ​​​​​​​Ans:( A ) 
    
  6. print("hospital".find("p")) 顯示的結果為何?

    ​​​​​​​(A)-1 (B)0 (C)3 (D)4
    
    ​​​​​​​Ans:( C ) 
    
  7. 下列何者不可能是 print(random.randint(1,10)) 的顯示結果?

    ​​​​​​​(A)0 (B)5 (C)8 (D)10
    
    ​​​​​​​Ans:( A )
    
  8. 下列何者不可能是 print(random.randrange(0,15,3)) 的顯示結果?

    ​​​​​​​(A)0 (B)3 (C)12 (D)15
    
    ​​​​​​​Ans:( D )
    
  9. 下列哪一個函式可讓程式停止執行一段時間?

    ​​​​​​​(A)time (B)sleep (C) perf_counter (D)localtime
    
    ​​​​​​​Ans:( B )
    
  10. localtime 傳回的 tm_min 資料範圍為何?

    ​​​​​​​​(A)1到60 (B)0到60 (C)0到59 (D)1到59
    ​​​​​​​​
    ​​​​​​​​Ans:( C ) 
    
#print(max([4,8,3,9,2,6])) 顯示的結果為何?

print(max([4,8,3,9,2,6])) 
​​​​9
#print(pow(2,5,7)) 顯示的結果為何?

print(pow(2,5,7)) 
​​​​4
#print("hospital".replace("s","t")) 顯示的結果為何?

print("hospital".replace("s","t")) 
​​​​hotpital
#print("hospital".startswith("ho")) 顯示的結果為何?

print("hospital".startswith("ho")) 
​​​​True
#print("hospital".find("p")) 顯示的結果為何?

print("hospital".find("p")) 
​​​​3