Day 7 - 프로젝트 Hangman
● Day 7 프로젝트_Hangman
Step1. 입력한 문자와 동일한지 아닌지 판별
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(f"Pssst, the solution is {chosen_word}.")
guess = input("Guess a letter: ").lower()
for letter in chosen_word:
if letter == guess:
print("Right")
else:
print("Wrong")
Step2. 입력한 문자 올바른 위치에 표시
(+) 부족한 개념
for 문자 in 문자열/배열:
print(문자)
=> 문자열/배열의 문자/원소 하나씩 출력
for i in range(0,10,1):
print(i)
=> 0부터 9까지 1 간격으로 출력
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(f"Pssst, the solution is {chosen_word}.")
display = []
guess = input("Guess a letter: ").lower()
for letter in chosen_word:
if letter == guess:
display += guess
else:
display += "_"
print(display)
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(f"Pssst, the solution is {chosen_word}.")
display = []
for letter in chosen_word:
display += "_"
print(display)
guess = input("Guess a letter: ").lower()
for letter in chosen_word:
if letter == guess:
display += guess
print(display)
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(f"Pssst, the solution is {chosen_word}.")
display = []
word_length = len(chosen_word)
for _ in range(word_length):
display += "_"
print(display)
guess = input("Guess a letter: ").lower()
for i in range(word_length):
letter = chosen_word[i]
if letter == guess:
display[i] = guess
print(display)
Step3. 계속 문자 입력하기
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
print(f"Pssst, the solution is {chosen_word}.")
display = []
word_length = len(chosen_word)
for _ in range(word_length):
display += "_"
print(display)
end_of_game = False
while not end_of_game:
guess = input("Guess a letter: ").lower()
for i in range(word_length):
letter = chosen_word[i]
if letter == guess:
display[i] = guess
print(display)
if "_" not in display:
end_of_game = True
print("You win!")
# if "_" not in display: 특정 목록 안에 특정 요소 존재 여부 확인
=> display라는 리스트 안에 _ 라는 요소 있는지 확인
Step 4. 일정 횟수 이상 틀린 문자 입력 시, 게임 종료
import random
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']
print(f"Pssst, the solution is {chosen_word}.")
display = []
word_length = len(chosen_word)
for _ in range(word_length):
display += "_"
print(display)
end_of_game = False
lives = 6
while not end_of_game:
guess = input("Guess a letter: ").lower()
if guess in chosen_word:
for i in range(word_length):
letter = chosen_word[i]
if letter == guess:
display[i] = guess
print(display)
elif guess not in chosen_word:
lives -= 1
if lives != 0:
print(stages[lives])
print(f"You have {lives} lives left...")
else:
print(stages[lives])
print("You lose...")
end_of_game = True
if "_" not in display:
end_of_game = True
print("You win!")
Step5. 모듈화 및 함수 선언
import random
from replit import clear
from hagman_words import word_list
chosen_word = random.choice(word_list)
# import hagman_words
# chosen_word = random.choice(hanman_words.word_list)
from hanman_art import stages, logo
print(logo)
print(f"Pssst, the solution is {chosen_word}.")
word_length = len(chosen_word)
display = []
for _ in range(word_length):
display += "_"
print(display)
end_of_game = False
lives = 6
def show_display():
print(stages[lives])
print(display)
print(f"You have {lives} lives left.")
def false_value():
global lives
lives -= 1
show_display()
if lives == 0:
clear()
print(stages[lives])
print("You lose...")
global end_of_game
end_of_game = True
while not end_of_game:
guess = input("Guess a letter: ").lower()
clear()
if guess not in chosen_word:
print(f"You guessed {guess}, that's not in {guess}.")
false_value()
if guess in display:
print(f"You've already guessed {guess}.")
false_value()
if guess in chosen_word:
print(f"You guessed {guess}, that's in {guess}.")
for i in range(word_length):
letter = chosen_word[i]
if letter == guess:
display[i] = guess
show_display()
if "_" not in display:
end_of_game = True
clear()
print(display)
print("\nYou win!")
(파일 구조)
1) 화면 비우기
# from replit import clear
# clear()
2) 모듈 불러오기
# from hanman_art import stages, logo
# print(logo)
or
# import hagman_words
# chosen_word = random.choice(hanman_words.word_list)
3) 전역변수 & 지역변수
end_of_game = False
lives = 6
def false_value():
global lives
lives -= 1
show_display()
if lives == 0:
clear()
print(stages[lives])
print("You lose...")
global end_of_game
end_of_game = True
=> 전역변수를 사용자 함수에서 사용하기 위해서는 global을 붙여주어야한다.
● GIT URL : https://github.com/yooyijun15/Python/tree/main/Day7
Python/Day7 at main · yooyijun15/Python
Contribute to yooyijun15/Python development by creating an account on GitHub.
github.com