티스토리 뷰

Test/Python(20220101~)

Day7_Hangman Game2

kiostory 2022. 1. 8. 22:50
from replit import clear
import hangman_art
import hangman_words
import random
print(hangman_art.logo)
chosen_word=random.choice(hangman_words.word_list) #or from hangman_words import word_list 후 hangman_words.을 제외할 수도 있다
print(f'Pssst, the solution is {chosen_word}.')
display=[]
for letter in chosen_word:
    display+="_"
print(display)
end_of_game = False
lives = 6
guessed_letter=[]
while not end_of_game:
    print(guessed_letter)
    did_match = False
    already_guessed = False
    guess=input("Guess a letter : ").lower()
    clear()
    for point in range(len(chosen_word)):
        if guess not in guessed_letter and guess == chosen_word[point] :
            display[point] = guess
            print(hangman_art.stages[lives])
            did_match = True
        elif guess in guessed_letter and guess == chosen_word[point] :
            print(f"{guess} is already guessed letter!")
            print(hangman_art.stages[lives])
            already_guessed = True
            did_match = True
    guessed_letter.append(guess)
    if already_guessed == False and did_match == False:
        lives-=1
        print(hangman_art.stages[lives])
        if lives == 0:
            end_of_game = True
            print("You lose.")
            print(f"The solution is : {chosen_word}")
    print(display)
    if "_" not in display:
        end_of_game = True
        print("You win.")
# kio2 original----------------------------------------------------------
# import hangman_art
# import hangman_words
# #TODO-1 - Randomly choose a word from the word_list and assign it to a variable called chosen_word.
# import random
# #chosen_word = word_list[random.randint(0,len(word_list)-1)]
# print(hangman_art.logo)
# chosen_word=random.choice(hangman_words.word_list)
# print(f'Pssst, the solution is {chosen_word}.')
# display=[]
# for letter in chosen_word:
# #for _ in chosen_word:
# #for _ in range(len(chosn_word))
#     display+="_"
# print(display)
# #print(f"{word_list[chosen_word]}")
# # count = len(chosen_word
# # while (count>0):
# end_of_game = False
# lives = 6
# guessed_letter=[]
# while not end_of_game:
#     #TODO-2 - Ask the user to guess a letter and assign their answer to a variable called guess. Make guess lowercase.
#     print(f"guessed_letter : {guessed_letter}")
#     did_match = False
#     already_guessed = False
#     #print(hangman_art.stages[lives])
#     guess=input("Guess a letter : ").lower()
#     #print(guess)
#     #TODO-3 - Check if the letter the user guessed (guess) is one of the letters in the chosen_word.
#     for point in range(len(chosen_word)):
#     #for point in range(0,len(chosen_word)):
#     #for letter in chosen_word:
#         if guess == chosen_word[point] and guess not in guessed_letter:
#             display[point] = guess
#             did_match = True
#         elif guess in guessed_letter:
#             print("It\'s already guessed letter!")
#             guessed_letter.append(guess)
#             already_guessed = True
#     if already_guessed == False and did_match == False:
#     #if guess not in chosen_word:
#         lives-=1
#         print(hangman_art.stages[lives])
#         if lives == 0:
#             end_of_game = True
#             print("You lose.")
#             print(f"The solution is : {chosen_word}")
#     print(display)
#     if "_" not in display:
#         end_of_game = True
#         print("You win.")
#-----------------------------------
# #Step 5
# import random
# #TODO-1: - Update the word list to use the 'word_list' from hangman_words.py
# #Delete this line: word_list = ["ardvark", "baboon", "camel"]
# chosen_word = random.choice(word_list)
# word_length = len(chosen_word)
# end_of_game = False
# lives = 6
# #TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.
# #Testing code
# print(f'Pssst, the solution is {chosen_word}.')
# #Create blanks
# display = []
# for _ in range(word_length):
#     display += "_"
# while not end_of_game:
#     guess = input("Guess a letter: ").lower()
#     #TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
#     #Check guessed letter
#     for position in range(word_length):
#         letter = chosen_word[position]
#         print(f"Current position: {position}\n Current letter: {letter}\n Guessed letter: {guess}")
#         if letter == guess:
#             display[position] = letter
#     #Check if user is wrong.
#     if guess not in chosen_word:
#         #TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
#         lives -= 1
#         if lives == 0:
#             end_of_game = True
#             print("You lose.")
#     #Join all the elements in the list and turn it into a String.
#     print(f"{' '.join(display)}")
#     #Check if user has got all letters.
#     if "_" not in display:
#         end_of_game = True
#         print("You win.")
#     #TODO-2: - Import the stages from hangman_art.py and make this error go away.
#     print(hangman_art.stages[lives])

'Test > Python(20220101~)' 카테고리의 다른 글

Day8_페인트 면적 계산 (Function)  (0) 2022.01.09
Day8_입력값이 있는 함수  (0) 2022.01.09
Day7_Hangman Game  (0) 2022.01.08
Day6_미로탈출  (0) 2022.01.07
Day6_While loop  (0) 2022.01.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함