1. 단어장 만들기
with open('vocabulary.txt', 'w') as f:
while True:
english_word = input('영어 단어를 입력하세요: ')
if english_word == 'q':
break
korean_word = input('한국어 뜻을 입력하세요: ')
if korean_word == 'q':
break
f.write('{}: {}\n'.format(english_word, korean_word))
2. 단어 퀴즈
모범 답안
with open('vocabulary.txt', 'r') as f:
for line in f:
data = line.strip().split(": ")
english_word, korean_word = data[0], data[1]
# 유저 입력값 받기
guess = input("{}: ".format(korean_word))
# 정답 확인하기
if guess == english_word:
print("맞았습니다!\n")
else:
print("아쉽습니다. 정답은 {}입니다.\n".format(english_word))
3. 랜덤 퀴즈
모범 답안
import random
# 사전 만들기
vocab = {}
with open('vocabulary.txt', 'r') as f:
for line in f:
data = line.strip().split(": ")
english_word, korean_word = data[0], data[1]
vocab[english_word] = korean_word
# 목록 가져오기
keys = list(vocab.keys())
# 문제 내기
while True:
# 랜덤한 문제 받아오기
index = random.randint(0, len(keys) - 1)
english_word = keys[index]
korean_word = vocab[english_word]
# 유저 입력값 받기
guess = input("{}: ".format(korean_word))
# 프로그램 끝내기
if guess == 'q':
break
# 정답 확인하기
if guess == english_word:
print("정답입니다!\n")
else:
print("틀렸습니다. 정답은 {}입니다.\n".format(english_word))
'자동제어 > Python for robotics' 카테고리의 다른 글
숫자야구 (0) | 2023.02.19 |
---|---|
로또 시뮬레이션 (0) | 2023.02.19 |
평균매출구하기(파일열기 모듈, strip, split 함수) (0) | 2023.02.19 |
숫자맞히기 게임 (0) | 2023.02.19 |
datetime 모듈 (1) | 2023.02.19 |