아래는 윈도우 OS에서 중학생 수준의 영단어 시험 문제를 풀 수 있는 파이썬 프로그램의 예제 코드입니다. 이 코드는 가로 400픽셀, 세로 300픽셀 크기의 창을 열고, 랜덤으로 영단어와 올바른 한글 보기를 출력하며 사용자의 선택에 대한 피드백을 제공합니다. 10개의 문제를 생성합니다.
import tkinter as tk
import random
# 영어 단어와 해당되는 한글 보기 쌍을 리스트로 저장
word_pairs = [
("apple", "사과"),
("banana", "바나나"),
("carrot", "당근"),
("dog", "개"),
("elephant", "코끼리"),
("flower", "꽃"),
("guitar", "기타"),
("hamburger", "햄버거"),
("ice cream", "아이스크림"),
("jacket", "자켓")
]
# 퀴즈 횟수
total_questions = 10
# 퀴즈 진행 상태를 저장하는 변수
current_question = 0
# 정답 횟수
correct_answers = 0
# 랜덤한 문제 순서를 생성
random.shuffle(word_pairs)
# 정답 확인 함수
def check_answer(choice):
global current_question, correct_answers
# 사용자의 선택과 실제 정답 비교
if choice == word_pairs[current_question][1]:
feedback_label.config(text="정답", fg="green")
correct_answers += 1
else:
feedback_label.config(text="다시 선택해주세요.", fg="red")
# 다음 문제로 진행
current_question += 1
# 모든 문제를 다 풀었을 때 결과 출력
if current_question == total_questions:
result_label.config(text=f"정답 수: {correct_answers}/{total_questions}")
root.after(2000, root.destroy) # 2초 후 창 닫기
else:
show_question()
# 문제 출력 함수
def show_question():
word_label.config(text=word_pairs[current_question][0])
choices = [pair[1] for pair in word_pairs[current_question:current_question + 4]]
random.shuffle(choices)
for i in range(4):
choice_buttons[i].config(text=choices[i])
# Tkinter 창 생성
root = tk.Tk()
root.title("영어 단어 퀴즈")
root.geometry("400x300")
# 레이블 및 버튼 생성
word_label = tk.Label(root, text="", font=("Arial", 24))
word_label.pack(pady=20)
feedback_label = tk.Label(root, text="", font=("Arial", 16))
feedback_label.pack()
choice_buttons = []
for i in range(4):
button = tk.Button(root, text="", font=("Arial", 16), width=20, command=lambda i=i: check_answer(choice_buttons[i]["text"]))
button.pack(pady=5)
choice_buttons.append(button)
result_label = tk.Label(root, text="", font=("Arial", 16))
result_label.pack()
# 첫 번째 문제 표시
show_question()
root.mainloop()
위 코드를 복사하여 파이썬 스크립트로 저장하고 실행하면 원하는 기능을 가진 영어 단어 퀴즈 프로그램이 실행됩니다. 코드는 Tkinter를 사용하여 GUI를 생성하고 관리합니다. 퀴즈가 끝나면 정답 수가 표시되며 2초 후 창이 닫힙니다.
위처럼 결과 확인
조금 다듬을 필요는 있지만 그래도 구동은 가능하도록 만들어짐
'Code of AI' 카테고리의 다른 글
[ChatGPT] 벽돌깨기 게임 만들기 파이썬 코드 (0) | 2023.09.24 |
---|---|
[ChatGPT] 윈도우용 검색 프로그램 파이썬 코딩 (0) | 2023.09.21 |
[ChatGPT] 지뢰찾기 게임 만들기 II (수정) (0) | 2023.09.20 |
[ChatGPT] 지뢰찾기 게임 만들기 I (0) | 2023.09.20 |
[Bard & ChatGPT] 구구단 출력 윈도우 프로그램 파이썬 코딩 (0) | 2023.09.20 |