[Python 과제 LV. 3] 단어 맞추기 게임
#상황: 단어를 주어진 기회 안에 맞추는 게임을 만들기
# 문제:
- 컴퓨터가 랜덤으로 영어단어를 선택합니다.
- 영어단어의 자리수를 알려줍니다.
- ex ) PICTURE = 7자리
- 사용자는 A 부터 Z 까지의 알파벳 중에서 하나를 선택합니다.
- 맞출 경우 해당 알파벳이 들어간 자리를 전부 보여줍니다.
- 틀릴 경우 목숨이 하나 줄어듭니다.
- 사용자가 9번 틀리면 게임오버됩니다.
- 게임오버 되기 전에 영어단어의 모든 자리를 알아내면 플레이어의 승리입니다.
# 정답:
import random
word = random.choice(words_list)
print ("the word has {} characters".format(len(word)))
guesses = ''
turns = 10
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print (char,end="")
else:
print ("_",end="")
failed += 1
if failed == 0:
print ("You won")
break
guess = input("\nguess a character: ")
guesses += guess
if guess not in word:
turns -= 1
print ("Wrong")
print ("You have", + turns, 'more guesses')
if turns == 0:
print ("You Lose, the word was '{}'.".format(word))
'Python' 카테고리의 다른 글
[Python Libraries] pandas, numpy, matploilib, seaborn, scikit-learn, statsmodels, scipy, tensorflow, pytorch (0) | 2024.07.04 |
---|---|
[Python 101] 여러 확장자로 Data Frame 저장하기 (0) | 2024.07.04 |
[Python 101] 변수들과 데이터 관련 함수 (0) | 2024.07.04 |
[Python 과제 LV. 2] 스파르타 자판기 (0) | 2024.06.10 |
[Python 과제 LV. 1] 랜덤 닉네임 생성기 (0) | 2024.06.10 |