Skip to content
파이썬
2023.10.04 23:29

파이썬 랜덤으로 시험문제 풀기

조회 수 72497 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

파이썬 랜덤으로 시험문제 풀기

 

시험문제를 1-3.png 2-2.png 3-5.png . . . 

로 저장하는데,

 

앞의 번호와 -뒤에 숫자는 문제의 정답번호를 말하는 것입니다.

예를들어 1-3.png 는 1번문제의 정답은 3 입니다.

문제번호를 여러개를 저장한 다음 코딩을 실행하면 랜덤으로 문제가 추출됩니다.

 

1-3.png

 

2-1.png

 

3-3.png

 

4-4.png

 

5-5.png

 

6-2.png

 

7-3.png

 

8-1-모세혈관.2-산소.3-교환.png

 

 


import os
import random
from PIL import Image, ImageTk
import tkinter as tk
import pygame
 
# 이미지 폴더 경로
image_folder = r'E:\python\py_code\공\data\과학'
 
# 이미지 파일 목록 생성
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
 
# 하단 GUI 요소를 미리 생성
window = tk.Tk()
window.title("문제 맞추기 게임")
 
# 이미지 표시용 라벨
image_label = tk.Label(window)
image_label.pack(pady=10)
 
# 이미지와 확인 버튼을 포함하는 프레임 생성
frame = tk.Frame(window)
frame.pack()
 
question_label = tk.Label(frame, text="")
question_label.grid(row=0, column=0)
 
entry = tk.Entry(frame)
entry.grid(row=0, column=1, padx=10)
 
result_label = tk.Label(frame, text="")
result_label.grid(row=1, columnspan=2)
 
# Pygame 초기화 및 MP3 파일 경로 설정
pygame.init()
correct_sound = pygame.mixer.Sound(r'E:\python\py_code\공\ok.mp3')
incorrect_sound = pygame.mixer.Sound(r'E:\python\py_code\공\nono.mp3')
 
def play_correct_sound():
    correct_sound.play()
 
def play_incorrect_sound():
    incorrect_sound.play()
 
def show_random_question():
    # 랜덤하게 이미지 선택
    random_image = random.choice(image_files)
    image_path = os.path.join(image_folder, random_image)
   
    # 이미지 표시
    img = Image.open(image_path)
    img = img.resize((400, 400))
    img = ImageTk.PhotoImage(img)
    image_label.config(image=img)
    image_label.image = img
   
    # 정답 추출
    answer = int(random_image.split('-')[1].split('.')[0])
   
    # 사용자로부터 정답 입력 받기
    def check_answer(event=None):
        user_answer = int(entry.get())
        if user_answer == answer:
            result_label.config(text="정답을 맞췄어요.")
            play_correct_sound()  # 정답 소리 재생
            show_random_question()
        else:
            result_label.config(text="다시 한번 풀어보세요.")
            play_incorrect_sound()  # 오답 소리 재생
   
    # 엔터 키로 정답 확인
    entry.bind("<Return>", check_answer)
   
    # 하단 GUI 업데이트
    entry.delete(0, tk.END)
    result_label.config(text="")
   
    # 하단 GUI 생성
    question_label.config(text="이미지에 나타난 숫자는 무엇일까요?")
   
    # 확인 버튼 생성 (오른쪽에 위치)
    check_button = tk.Button(frame, text="확인", command=check_answer)
    check_button.grid(row=0, column=2)
 
# 초기 문제 표시
show_random_question()
 
# Tkinter 이벤트 루프 시작
window.mainloop()
 
# pygame 종료
pygame.mixer.quit()

 


 

로그인 후 댓글쓰기가 가능합니다.

?

List of Articles
번호 분류 제목 날짜 조회 수
60 AVR 아두이노 전압측정(오실레이터가 없을때 활용) file 2013.10.18 27385
59 AVR 555 TIMER AM TRANSMITTER CIRCUIT(AM 송신 회로) file 2014.09.20 23298
58 AVR ARDUINO 명령어 모음 3 2012.07.24 22842
57 AVR 열전도율 높은 금속 순위 1 2013.06.25 22833
56 AVR 정전압 레귤레이터 1 5 2012.08.11 20392
55 AVR LTC871 DC to DC 부스터 컨버터 스텝업 (LTC1871 DC-DC Boost converter Adjustable Step-Up High Power Supply Module Red LED Voltage meter) file 2014.08.14 19509
54 AVR 태양광 전지필름을 이용한 스마트폰 충전기 만들기 2 file 2012.10.31 19487
53 AVR 열전 소자[ thermoelement , 熱電素子 ] file 2013.06.25 16724
52 AVR 아두이노 업로드 통신 설정방법 file 2019.06.13 15745
51 AVR 아두이노(우노UNO)와 HC-06 블루투스 모듈 연결하기(성공) 2 5 file 2012.08.17 13875
50 AVR 아두이노 Uno + Thermistor 온도센서 사용해보기 3 file 2012.08.05 13759
49 AVR 회로도, 마이컴, 브레드보드 사용방법 등 3 2012.07.18 13370
48 AVR 아두이노로 할 수 있는 아이디어 및 프로젝트, 기타 정보 모음 2 1 2012.08.30 13159
47 AVR 아두이노 카페 2013.08.17 12556
46 AVR 가변저항 3 file 2012.07.18 12130
45 AVR Arduino 특정시간 제어와 릴레이 다루기 2019.06.13 11937
44 AVR 아두이노 Uno + Cds Cell 5파이 광(빛감지) 감지센서 사용해보기 1 1 file 2012.08.05 11908
43 AVR 5V로 정전압 출력하기 2013.06.28 11837
42 AVR 안드로이드 스마트폰 + 블루투스 통신 + 아두이노 자동차 2 2012.08.03 11560
41 AVR 아두이노 예제) LED 속도 가속 또는 랜덤화 하기 + 시리얼 통신까지 20 2012.08.04 11292
Board Pagination Prev 1 2 3 Next
/ 3

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소