import random
for i in range(3):
# 生成1到9之間的整數亂數
first = random.randint(1, 9)
second = random.randint(1, 9)
answer = int(input(f"{first} + {second} ="))
if (first + second == answer):
print("答對了")
else:
print("答錯了")
加法練習: 修改add.py產生5次三個0-9的整數亂數,讓小朋友回答相加的結果 (10%)
import random
for i in range(3):
# 生成1到9之間的整數亂數
first = random.randint(1, 9)
second = random.randint(1, 9)
# 如果第一個數較小,則兩數對調
if (first < second):
first, second = second, first
answer = int(input(f"{first} - {second} ="))
if (first - second == answer):
print("答對了")
else:
print("答錯了")
減法練習:上面的substract.py產生3次兩個0-9的整數亂數,讓小朋友回答相減的結果
exams = [{"question":"氫的化學符號", "level":"B", "answer":"D", "options":["(A)He","(B)T","(C)Ti","(D)H"]},
{"question":"氧的化學符號", "level":"B", "answer":"B", "options":["(A)Y","(B)O","(C)Yi","(D)I"]},
{"question":"氦的化學符號", "level":"B", "answer":"B", "options":["(A)Hi","(B)He","(C)H","(D)Ha"]},
{"question":"請選擇一個鹼金屬元素", "level":"A", "answer":"C", "options":["(A)鎂","(B)鈣","(C)鉀","(D)鐳"]},
{"question":"請選擇一個惰性/稀有氣體", "level":"A", "answer":"A", "options":["(A)氦","(B)氫","(C)氮","(D)氯"]}
]
for exam in exams:
print(exam["question"])
for option in exam["options"]:
print (option)
entered = input()
if (entered.upper() == exam["answer"]):
print("正確")
else:
print("答錯了")
其中「"level":"B"」就是每一題的難度
改寫chemistry.py,利用程式讓小朋友進行單選題練習,最後告訴小朋友答對幾次。 (10%)