Skip to content

Instantly share code, notes, and snippets.

@41023137
41023137 / w1_hw_2
Created March 6, 2025 18:07
w1_hw_2
from browser import document, html
num = 11
# 利用 document[] 取得 id 為 "brython_div1" 的位置, 然後與 brython_div1 變數對應
brython_div1 = document["brython_div1"]
for i in range(num):
# 利用 Brython 的 "<=" 特殊符號, 將右值帶有字串與超文件內容物件插入 brython_div1 變數所指定的位置
# 其中利用 html.A 建立 anchor 標註物件, 第一個變數為 anchor 字串, 第二個變數則為 hyper text reference
brython_div1 <= str(i+1) + ". " + html.A("ag" + str(i+1), href="https://mdecd2025.github.io/2a-" + "ag" + str(i+1))
brython_div1 <= " | " + html.A("repo", href="https://github.com/mdecd2025/2a-" + "ag" + str(i+1))
# 利用 html.BR() 插入 break 標註
@41023137
41023137 / w1_hw_1
Created March 6, 2025 18:06
w1_hw_1
# 從 Brython 程式庫中的 browser 導入 html, 可用來輸出超文件內容
# 而所導入的 document 可以指向網頁中的特定 id 內容
from browser import html, document
# 建立 url 變數與網站中 2a 學員的學號與帳號資料 URL 網誌字串對應
url = "https://mde.tw/list/2a.txt"
# 利用 open() 物件中的 readlines() 方法, 取出網站中的資料並逐行放入數列中, 因此 data 的資料型別為數列
data = open(url).readlines()
# 進行資料查驗時, 印出 data 數列內容
#print(data)
# 因為學員資料中的第一列為標題, 因此有效資料從索引值 1 開始
@41023137
41023137 / w16_exam3
Created December 25, 2024 04:10
w16_exam3
from browser import html
from browser import document as doc
# 利用 html 建立 canvas 超文件物件
canvas = html.CANVAS(width=300, height=300) # 設定迷宮畫布大小
brython_div = doc["brython_div1"]
brython_div <= canvas
# 取得 canvas 的繪圖上下文
ctx = canvas.getContext("2d")
@41023137
41023137 / w16_exam2 Zoom
Created December 25, 2024 03:41
w16_exam2 Zoom
from browser import html
from browser import document as doc
# 利用 html 建立 canvas 超文件物件
canvas = html.CANVAS(width=800, height=800) # 設定迷宮畫布大小
brython_div = doc["brython_div1"]
brython_div <= canvas
# 取得 canvas 的繪圖上下文
ctx = canvas.getContext("2d")
@41023137
41023137 / w16_exam2
Created December 25, 2024 03:10
w16_exam2
from browser import html
from browser import document as doc
# 利用 html 建立 canvas 超文件物件
canvas = html.CANVAS(width=300, height=300) # 設定迷宮畫布大小
brython_div = doc["brython_div1"]
brython_div <= canvas
# 取得 canvas 的繪圖上下文
ctx = canvas.getContext("2d")
@41023137
41023137 / w16_exam1
Created December 25, 2024 02:05
w16_exam1
from browser import html
from browser import document as doc
# 利用 html 建立 canvas 超文件物件
canvas = html.CANVAS(width=500, height=500)
brython_div = doc["brython_div1"]
brython_div <= canvas
# 取得 canvas 的繪圖上下文
ctx = canvas.getContext("2d")
@41023137
41023137 / w15_4
Created December 18, 2024 03:15
addto_avoid_8()
def addto_avoid_8(init, upto):
# 初始化總和為 0
sum_avoid_8 = 0
# 遍歷從 init 到 upto 之間的數字
for i in range(init, upto + 1):
if '8' not in str(i): # 檢查數字是否包含 '8'
sum_avoid_8 += i # 累加不包含 '8' 的數字
print(f"從 {init} 累加到 {upto} 避開包含 '8' 的數字後的總和為: {sum_avoid_8}")
@41023137
41023137 / w15_3
Created December 18, 2024 03:03
addto_only_even()
def addto_only_even(init, upto):
# 初始化總和為 0
sum_even = 0
# 遍歷從 init 到 upto 之間的數字,並只累加偶數
for i in range(init, upto + 1):
if i % 2 == 0: # 檢查是否為偶數
sum_even += i # 累加偶數
print(f"從 {init} 累加到 {upto} 的偶數總和為: {sum_even}")
@41023137
41023137 / w15_2
Created December 18, 2024 02:45
addto()
# sum 初始值設為 0
sum = 0
def addto(init, upto):
global sum
#init = 1
#upto = 100
# 利用 for 重複迴圈與變數加法進行累加
for i in range(init, upto+1):
sum = sum + i
print("從" + str(init) + "累加到" + str(upto) + "=" + str(sum))
@41023137
41023137 / w15
Created December 18, 2024 01:53
從 1 累加到 100
# sum 初始值設為 0
sum = 0
init = 1
upto = 100
# 利用 for 重複迴圈與變數加法進行累加
for i in range(init, upto+1):
sum = sum + i
print("從" + str(init) + "累加到" + str(upto) + "=" + str(sum))