練習以下題目

add.py

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("答錯了")

Untitled

substract.py

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("答錯了")

chemistry.py


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"」就是每一題的難度