Test/Python(20220101~)

Day7_Hangman Game

kiostory 2022. 1. 8. 12:53

For & While loops

IF/Else

Lists

Strings

Range

Modules

 

총 망라하여 생각할 필요 있음

 

 

 

 

https://drive.google.com/file/d/14SDAxEl7ehqtyvWceCrbtlvzQsldPxSa/view?usp=sharing

 

 

 

 

* My first code 

 

word_list = ["aardvark", "baboon", "camel"]

#TODO-1 - Randomly choose a word from the word_list and assign it to a variable called chosen_word.

#TODO-2 - Ask the user to guess a letter and assign their answer to a variable called guess. Make guess lowercase.

#TODO-3 - Check if the letter the user guessed (guess) is one of the letters in the chosen_word.
#------------------------------------------------------------------------------------------------------------------
stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']





#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)]
chosen_word=random.choice(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

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.
did_match = False
print(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]:
display[point] = guess
did_match = True
 
if did_match == False:
#if guess not in chosen_word:
lives-=1
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.")