國中資科三課本
======
### [範 例-哈囉程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E7%A8%8B%E5%BC%8F%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E5%90%8D%E5%AD%97%EF%BC%8C%E5%92%8C%E4%BD%BF%E7%94%A8%E8%80%85%E6%89%93%E6%8B%9B%E5%91%BC&actor=python)
:::warning
設計程式讓使用者輸入名字,和使用者打招呼
:::
```python=
name = input("請輸入您的名字:")
print("您好," + name + "!歡迎使用本程式。")
```
### [範 例-兩數之和程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E7%A8%8B%E5%BC%8F%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E5%85%A9%E5%80%8B%E6%95%B8%E5%AD%97%EF%BC%8C%E9%A1%AF%E7%A4%BA%E5%85%A9%E6%95%B8%E7%9B%B8%E5%8A%A0%E7%9A%84%E5%92%8C&actor=python)
:::warning
設計程式讓使用者輸入兩個數字,顯示兩數相加的和
:::
```python=
num1 = input("請輸入第一個數字:")
num2 = input("請輸入第二個數字:")
num1 = int(num1)
num2 = int(num2)
result = num1 + num2
print("兩數相加的和為:", result)
```
### [範 例-求平均數程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E7%A8%8B%E5%BC%8F%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E5%85%A9%E5%80%8B%E6%95%B8%E5%AD%97%EF%BC%8C%E9%A1%AF%E7%A4%BA%E5%85%A9%E6%95%B8%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC&actor=python)
:::warning
設計程式讓使用者輸入兩個數字,顯示兩數的平均值
:::
```python=
num1 = float(input("請輸入第一個數字:"))
num2 = float(input("請輸入第二個數字:"))
total = num1 + num2
average = total / 2
print("兩數的平均值為:", average)
```
### [範 例-計算學期成績程式](https://chat.webduino.io/index.html#guest?prompt=%E8%AB%8B%E8%A8%AD%E8%A8%88%E4%B8%80%E5%80%8B%E7%A8%8B%E5%BC%8F%EF%BC%8C%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E5%90%84%E9%A0%85%E6%88%90%E7%B8%BE%E5%BE%8C%EF%BC%8C%E5%86%8D%E5%B0%87%E5%90%84%E9%A0%85%E6%88%90%E7%B8%BE%E8%BD%89%E6%8F%9B%E7%82%BA%E5%AD%B8%E6%9C%9F%E6%88%90%E7%B8%BE%EF%BC%8C%E4%B8%A6%E5%88%A4%20%E6%96%B7%E5%AD%B8%E6%9C%9F%E6%88%90%E7%B8%BE%E6%98%AF%E5%90%A6%E5%8F%8A%E6%A0%BC%3F(%E5%85%B6%E4%B8%AD%EF%BC%8C%E4%BD%9C%E6%A5%AD%E6%88%90%E7%B8%BE%E5%8D%A0%2040%25%EF%BC%8C%E6%B8%AC%E9%A9%97%E6%88%90%E7%B8%BE%E5%8D%A0%2040%25%EF%BC%8C%E5%B9%B3%E6%99%82%E6%88%90%E7%B8%BE%E5%8D%A0%2020%25%EF%BC%8C%E5%AD%B8%E6%9C%9F%E6%88%90%E7%B8%BE%2060%20%E5%88%86%E7%82%BA%E5%8F%8A%E6%A0%BC%E5%88%86%E6%95%B8%E3%80%82)&actor=python)
:::warning
請設計一個程式,讓使用者輸入各項成績後,再將各項成績轉換為學期成績,並判 斷學期成績是否及格?(其中,作業成績占 40%,測驗成績占 40%,平時成績占 20%,學期成績 60 分為及格分數。)
:::
```python=
homework = float(input("請輸入作業成績:"))
test = float(input("請輸入測驗成績:"))
daily = float(input("請輸入平時成績:"))
semester_score = homework * 0.4 + test * 0.4 + daily * 0.2
if semester_score >= 60:
result = "及格"
else:
result = "不及格"
print("學期成績為:", semester_score)
print("學期成績", result)
```
### [範 例-累加計算程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E7%A8%8B%E5%BC%8F%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E6%95%B8%E5%AD%97%20n%20%E5%BE%8C%EF%BC%8C%E5%86%8D%E8%A8%88%E7%AE%97%201%20%2B%202%20%2B%203%20%2B...%2B%20n%20%E7%9A%84%E5%80%BC&actor=python)
:::warning
設計程式讓使用者輸入數字 n 後,再計算 1 + 2 + 3 +...+ n 的值
:::
```python=
n = int(input("請輸入一個數字:"))
total = 0
for i in range(1, n+1):
total += i
print("1 到 {} 的總和為:{}".format(n, total))
```
### [範 例-密碼程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E5%AF%86%E7%A2%BC%E9%A9%97%E8%AD%89%E6%A9%9F%E5%88%B6%EF%BC%8C%E6%A2%9D%E4%BB%B6%E5%A6%82%E4%B8%8B%0A1%20%E8%8B%A5%E7%AC%AC%E4%B8%80%E6%AC%A1%E8%BC%B8%E5%85%A5%E5%AF%86%E7%A2%BC%E9%8C%AF%E8%AA%A4%E5%BE%8C%EF%BC%8C%E5%8F%AF%E5%86%8D%E9%87%8D%E8%A4%87%E5%98%97%E8%A9%A6%E8%BC%B8%E5%85%A5%E5%85%A9%E6%AC%A1%0A2%20%E8%8B%A5%E4%B8%89%E6%AC%A1%E5%AF%86%E7%A2%BC%E9%83%BD%E9%8C%AF%E8%AA%A4%EF%BC%8C%E8%B7%B3%E5%87%BA%E4%BD%BF%E7%94%A8%E8%80%85%E5%B8%B3%E8%99%9F%E8%A2%AB%E9%8E%96%E5%AE%9A%E7%9A%84%E8%A8%8A%E6%81%AF&actor=python)
:::warning
設計密碼驗證機制,條件如下
1 若第一次輸入密碼錯誤後,可再重複嘗試輸入兩次
2 若三次密碼都錯誤,跳出使用者帳號被鎖定的訊息
:::
```python=
password = "1234" # 設定密碼
count = 0 # 計數器
while True:
input_password = input("請輸入密碼:")
if input_password == password:
print("密碼正確")
break
else:
count += 1
if count == 3:
print("使用者帳號被鎖定")
break
else:
print("密碼錯誤,請重新輸入")
```
### [範 例-任意數的所有因數程式](https://chat.webduino.io/index.html#guest?prompt=%E8%A8%AD%E8%A8%88%E7%A8%8B%E5%BC%8F%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E4%B8%80%E5%80%8B%E6%95%B8%E5%AD%97%E5%BE%8C%EF%BC%8C%E6%89%BE%E5%87%BA%E8%A9%B2%E6%95%B8%E5%AD%97%E6%89%80%E6%9C%89%E5%9B%A0%E6%95%B8&actor=python)
:::warning
設計程式讓使用者輸入一個數字後,找出該數字所有因數
:::
```python=
num = int(input("請輸入一個數字:"))
factors = []
for i in range(1, num+1):
if num % i == 0:
factors.append(i)
print(f"{num}的因數有:{factors}")
```
### [範 例-抽獎程式](https://chat.webduino.io/index.html#guest?prompt=%E6%9C%89%E4%B8%89%E5%8D%81%E4%BA%BA%E5%8F%83%E5%8A%A0%E6%91%B8%E7%8D%8E%E6%B4%BB%E5%8B%95%EF%BC%8C%E5%88%86%E5%88%A5%E6%9C%89%E7%B7%A8%E8%99%9F%201%20%E2%88%BC%2030%20%E7%9A%84%E6%91%B8%E7%8D%8E%E5%88%B8%EF%BC%8C%E9%80%8F%E9%81%8E%E9%9B%BB%E8%85%A6%E9%80%B2%E8%A1%8C%E6%8A%BD%E7%8D%8E%EF%BC%8C%E5%BE%9E%E4%B8%AD%E6%8A%BD%20%E5%87%BA%E4%B8%89%E4%BD%8D%E5%B9%B8%E9%81%8B%E5%85%92%E9%A0%92%E7%99%BC%E7%8D%8E%E5%93%81&actor=python)
:::warning
有三十人參加摸獎活動,分別有編號 1 ∼ 30 的摸獎券,透過電腦進行抽獎,從中抽 出三位幸運兒頒發獎品
:::
```python=
import random
participants = list(range(1, 31))
winners = random.sample(participants, 3)
print("恭喜以下三位幸運兒:")
for winner in winners:
print(winner)
```
### [範 例-1A2B 猜數字遊戲](https://chat.webduino.io/index.html#guest?prompt=%E8%AB%8B%E8%A8%AD%E8%A8%88%E4%B8%80%E5%80%8B%E7%A8%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BC%8C%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5%E7%9A%84%E6%95%B8%E5%AD%97%E5%AE%8C%E5%85%A8%E6%AD%A3%E7%A2%BA%EF%BC%8C%E5%89%87%E5%87%BA%E7%8F%BE%E3%80%8C%E6%82%A8%E7%AD%94%20%E5%B0%8D%E4%BA%86%EF%BC%8C%E6%AD%A3%E7%A2%BA%E7%AD%94%E6%A1%88%E6%98%AF%E2%8B%AF%E3%80%8D%3B%E6%B2%92%E7%8C%9C%E5%88%B0%E6%AD%A3%E7%A2%BA%E6%95%B8%E5%AD%97%EF%BC%8C%E5%89%87%E5%87%BA%E7%8F%BE%E3%80%8C%E4%BD%9C%E7%AD%94%E5%B7%B2%E9%81%94%208%20%E6%AC%A1%EF%BC%8C%E9%81%8A%E6%88%B2%E7%B5%90%E6%9D%9F%EF%BC%8C%E6%AD%A3%E7%A2%BA%E6%95%B8%20%E5%AD%97%E6%98%AF%E2%8B%AF%E3%80%8D%E3%80%82&actor=python)
:::warning
請設計一個程式,先隨機產生 4 個 0 ∼ 9 之間,且不重複的數字當正確答案,接著使用 者在畫面上輸入 4 個不重複的數字後,程式會將輸入的數字與答案進行比對,再用「幾 A 幾 B」的形式告訴使用者正確與否。其中,A 代表數字正確,且位置正確;B 代表數 字正確,但位置錯誤。如果在 8 次機會中,使用者輸入的數字完全正確,則出現「您答 對了,正確答案是⋯」;沒猜到正確數字,則出現「作答已達 8 次,遊戲結束,正確數 字是⋯」。
:::
```python=
import random
# 產生正確答案
answer = random.sample(range(10), 4)
# 計算幾 A 幾 B
def check(guess):
a = 0
b = 0
for i in range(4):
if guess[i] == answer[i]:
a += 1
elif guess[i] in answer:
b += 1
return a, b
# 讓使用者輸入數字
print("請輸入 4 個不重複的數字,範圍為 0~9")
guess_count = 0
while guess_count < 8:
guess = input("請輸入數字:")
if len(guess) != 4 or not guess.isdigit():
print("請輸入 4 個數字")
continue
guess = [int(x) for x in guess]
if len(set(guess)) != 4:
print("請輸入不重複的數字")
continue
a, b = check(guess)
print(f"{a}A{b}B")
if a == 4:
print("您答對了,正確答案是", answer)
break
guess_count += 1
else:
print("作答已達 8 次,遊戲結束,正確答案是", answer)
```