Python | レベル別練習問題:比較演算子

Python
スポンサーリンク

では、前の「比較演算子クイズ(全50問)」を GUI版(PySimpleGUI使用) に変換します。
このバージョンでは、
✅ ウィンドウに1問ずつ表示
✅ 「True / False」ボタンで回答
✅ 採点が自動表示
✅ 最後にスコア画面

になります!


GUI版:compare_quiz_gui.py

※ 実行前に一度 pip install PySimpleGUI をしておきましょう。

# compare_quiz_gui.py
# ===========================================
# Python 比較演算子クイズ(全50問)GUI版
# ===========================================
# 対象: プログラミング初心者
# ライブラリ: PySimpleGUI
# -------------------------------------------

import PySimpleGUI as sg

# -----------------------------
# 問題セット(式, 正解)
# -----------------------------
quiz_data = [
    ("5 == 5", True),
    ("5 != 3", True),
    ("4 < 2", False),
    ("10 > 5", True),
    ("7 >= 7", True),
    ("3 <= 2", False),
    ("'a' == 'A'", False),
    ("'abc' != 'abd'", True),
    ("10 == 10.0", True),
    ("True == 1", True),

    ("len('cat') == 3", True),
    ("'dog' < 'zebra'", True),
    ("'apple' > 'banana'", False),
    ("100 >= 200", False),
    ("5 * 2 == 10", True),
    ("10 / 2 != 5", False),
    ("'a' in 'apple'", True),
    ("'x' in 'apple'", False),
    ("'app' not in 'apple'", False),
    ("'pp' in 'apple'", True),

    ("[1,2] == [1,2]", True),
    ("[1,2] is [1,2]", False),
    ("x = [1,2]; y = x; x is y", True),
    ("(3 > 2) and (2 > 1)", True),
    ("(3 > 2) and (2 < 1)", False),
    ("(3 > 2) or (2 < 1)", True),
    ("not (5 > 2)", False),
    ("10 == 5 * 2", True),
    ("'Hello'.lower() == 'hello'", True),
    ("'HELLO'.isupper() == True", True),

    ("10 < 20 < 30", True),
    ("5 < 10 > 2", True),
    ("10 == 5 + 5 == True", False),
    ("not (3 < 5)", False),
    ("(3 < 5) and not (2 > 1)", False),
    ("(3 < 5) or not (2 > 1)", True),
    ("None is None", True),
    ("0 == False", True),
    ("'' == False", False),
    ("[] == False", False),

    ("'error' in 'log_error_message'", True),
    ("'ok' not in 'error_log'", True),
    ("10 != 10.0", False),
    ("'Python' > 'java'", True),
    ("'A' < 'a'", True),
    ("'あ' > 'い'", False),
    ("'10' == 10", False),
    ("type(10) == int", True),
    ("type(10.0) == float", True),
    ("isinstance('abc', str)", True),
]

# -----------------------------
# GUI 設定
# -----------------------------
sg.theme("DarkBlue3")

layout = [
    [sg.Text("Python 比較演算子クイズ", font=("Arial", 20), justification="center", expand_x=True)],
    [sg.Text("", key="-QNUM-", font=("Arial", 14))],
    [sg.Multiline("", key="-QUESTION-", size=(40, 4), font=("Consolas", 14), disabled=True)],
    [sg.Button("True (T)", key="TRUE", size=(10, 2), button_color=("white", "green")),
     sg.Button("False (F)", key="FALSE", size=(10, 2), button_color=("white", "red"))],
    [sg.Text("", key="-FEEDBACK-", font=("Arial", 13), text_color="yellow")],
    [sg.ProgressBar(len(quiz_data), orientation='h', size=(30, 20), key="-PROG-")],
    [sg.Button("終了", key="EXIT", size=(10, 1))]
]

window = sg.Window("比較演算子クイズ", layout, finalize=True)

# -----------------------------
# メインループ
# -----------------------------
index = 0
score = 0

def show_question(i):
    window["-QNUM-"].update(f"Q{i+1}/{len(quiz_data)}")
    expr, _ = quiz_data[i]
    window["-QUESTION-"].update(expr)
    window["-FEEDBACK-"].update("")

show_question(index)

while True:
    event, _ = window.read()
    if event in (sg.WIN_CLOSED, "EXIT"):
        break

    if event in ("TRUE", "FALSE"):
        user_ans = (event == "TRUE")
        expr, correct = quiz_data[index]
        if user_ans == correct:
            feedback = "✅ 正解!"
            score += 1
        else:
            feedback = f"❌ 不正解(正解は {correct})"
        window["-FEEDBACK-"].update(feedback)
        window["-PROG-"].update_bar(index + 1)

        index += 1
        if index < len(quiz_data):
            show_question(index)
        else:
            break

# -----------------------------
# 結果表示ウィンドウ
# -----------------------------
window.close()

percent = score / len(quiz_data) * 100
if percent == 100:
    msg = "🎉 パーフェクト!完璧です!"
elif percent >= 80:
    msg = "💪 よくできました!"
elif percent >= 50:
    msg = "👍 基礎は理解できています。"
else:
    msg = "📘 もう少し練習しましょう。"

sg.popup(
    "結果発表",
    f"正解数: {score} / {len(quiz_data)}\n正答率: {percent:.1f}%\n\n{msg}",
    font=("Arial", 14),
    title="結果",
)
Python

特徴まとめ

機能説明
✅ 出題形式1問ずつウィンドウに表示
🖱️ 回答操作「True」「False」ボタン
📊 進行状況バー進捗をバーで表示
🧮 採点結果最後にスコアと評価表示
🎨 テーマDarkBlue3(落ち着いた配色)
タイトルとURLをコピーしました