키즈 노트
키즈노트 #7 사자성어 퀴즈 전체 스코어, 최고 점수 기록
오리지날초이
2024. 6. 27. 18:18
import random
from colorama import init, Fore, Style
import webbrowser
import sqlite3
init()
# 파일 읽기
file_path = 'D:\\최정훈\\python sia quiz\\사자성어 퀴즈\\사자성어.txt'
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
# 사자성어와 설명을 분리하여 저장
proverbs = {}
for line in lines:
parts = line.strip().split(':')
if len(parts) == 2:
proverbs[parts[0].strip()] = parts[1].strip()
# 데이터베이스 설정
db_path = 'scores.db'
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, score INTEGER)''')
conn.commit()
# 사용자 이름 입력 받기
username = input("당신의 이름을 입력하세요: ")
# 랜덤으로 사자성어 선택 및 퀴즈 생성
def create_quiz(proverbs, num_choices=5):
correct_answer = random.choice(list(proverbs.keys()))
choices = random.sample(list(proverbs.keys()), num_choices - 1)
if correct_answer not in choices:
choices.append(correct_answer)
random.shuffle(choices)
return proverbs[correct_answer], choices, correct_answer
# 퀴즈 생성 및 사용자에게 제시
score = 0
correct = True
print("[사자성어 퀴즈]")
hint = []
while correct:
description, choices, correct_answer = create_quiz(proverbs)
# 사용자에게 문제 제시
print(f"\n설명: {description}")
print("보기:")
for i, choice in enumerate(choices, 1):
print(f"{i}. {choice}")
hint.append(choice)
print(Style.RESET_ALL, end="")
hint_num = int(input(Fore.BLUE + "몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : "))
if hint_num != 0:
url = f"https://hanja.dict.naver.com/#/search?query={hint[hint_num - 1]}&range=all"
webbrowser.get('c:/program files/internet explorer/iexplore.exe %s').open(url)
hint = []
# 사용자 입력 받기
user_answer = input(Fore.YELLOW + "정답 번호를 입력하세요: ")
print(Style.RESET_ALL, end="")
# 정답 확인
correct = choices[int(user_answer) - 1] == correct_answer
print(Fore.GREEN + "정답입니다!" if correct else Fore.RED + f"오답입니다. 정답은 {correct_answer}입니다.")
print(Style.RESET_ALL, end="")
if correct:
score += 1
# 스코어 데이터베이스에 저장
c.execute("INSERT INTO scores (name, score) VALUES (?, ?)", (username, score))
conn.commit()
print(Fore.RED + f"\n{username}님의 최종 스코어는 {score}점 입니다.")
# 모든 스코어 출력
print(Fore.BLUE + "\n[모든 사용자 점수]")
for row in c.execute("SELECT name, score FROM scores"):
print(f"{row[0]}: {row[1]}점")
print(Fore.BLUE + "\n[최고 점수]")
for row in c.execute("SELECT name, score FROM scores order by score desc limit 1"):
print(f"{row[0]}: {row[1]}점")
conn.close()
설명: 겉으로는 따르는 척 하나 속으로는 배신함을 이르는 말.
보기:
1. 金枝玉葉(금지옥엽)
2. 斷機之戒(단기지계)
3. 面從腹背(면종복배)
4. 傾國之色(경국지색)
5. 男負女戴(남부여대)
몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : 0
정답 번호를 입력하세요: 3
정답입니다!
설명: 같은 병의 환자끼리 서로 가엾게 여긴다는 뜻. 어려운 처지에 있는 사람끼리 동정하고 도와줌.
보기:
1. 家藏什物(가장집물)
2. 名實相符(명실상부)
3. 同病相憐(동병상련)
4. 同床異夢(동상이몽)
5. 南柯一夢(남가일몽)
몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : 0
정답 번호를 입력하세요: 3
정답입니다!
설명: 다른 일에 정신을 뺏겨 중요한 일을 소홀히 함을 이르는 말.
보기:
1. 讀書亡羊(독서망양)
2. 結者解之(결자해지)
3. 錦衣還鄕(금의환향)
4. 斷機之戒(단기지계)
5. 百年偕老(백년해로)
몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : 0
정답 번호를 입력하세요: 1
정답입니다!
설명: 하늘이 돕고 신이 도움.
보기:
1. 天佑神助(천우신조)
2. 傍若無人(방약무인)
3. 白骨難忘(백골난망)
4. 內憂外患(내우외환)
5. 千載一遇(천재일우)
몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : 0
정답 번호를 입력하세요: 1
정답입니다!
설명: 막대기 끝에 선 형세라는 뜻으로 아주 위태로운 상황을 이르는 말.
보기:
1. 粉骨碎身(분골쇄신)
2. 多岐亡羊(다기망양)
3. 落穽下石(낙정하석)
4. 竿頭之勢(간두지세)
5. 傍若無人(방약무인)
몇 번 힌트를 원하시나요? (0-5), 0은 힌트 없음 : 0
정답 번호를 입력하세요: 3
오답입니다. 정답은 竿頭之勢(간두지세)입니다.
ccc님의 최종 스코어는 28점 입니다.
모든 사용자 점수:
choi: 5점
choi2: 1점
choi3: 1점
ccc: 28점
최고 사용자 점수:
ccc: 28점
PS D:\최정훈\python sia quiz\사자성어 퀴즈>
728x90
반응형