Day 7 Goals: what we will make by the end of the day
How to break a Complex Problem down into a Flow Chart
#Step 1 word_list = ["aardvark", "baboon", "camel"]
#TODO-1 - Randomly choose a word from the word_list and assign it
#to a variable called chosen_word.
從word_list隨機取出一個單字給變數chosen_word
#TODO-2 - Ask the user to guess a letter and assign their answer to a
#variable called guess. Make guess lowercase.
請使用者隨意猜一個字母並轉換小寫,給予變數guess
#TODO-3 - Check if the letter the user guessed (guess) is one of the letters
#in the chosen_word.
檢查guess有沒有在chosen之中
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = rondom.chioce(word_list)
guess= input("Guess a letter: ").lower()
for letter in chosen_word:
if letter == guess:
print("Right")
else:
print("Wrong")
#Step 2
#TODO-1: - Create an empty List called display.
#For each letter in the chosen_word, add a "_" to 'display'.
#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.
根據隨機單字長度,產生等長的底線list
#TODO-2: - Loop through each position in the chosen_word;
#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].
套用猜測對的字母,取代掉底線
#TODO-3: - Print 'display' and you should see the guessed letter in the correct position and every other letter replace with "_".
#Hint - Don't worry about getting the user to guess the next letter. We'll tackle that in step 3.
顯示替換結果
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
#我
display = []
for letter in chosen_word:
display.append("_")
# display += "_"
print(display)
#老師
display =[]
for _ in range(len(chosen_word)):
display += "_"
print(display)
guess = input("Guess a letter: ").lower()
#我
for letter in chosen_word:
if letter == guess:
print("Right")
display.append(letter)
else:
print("Wrong")
display.append("_")
print(display)
#老師
for position in range(len(chosen_word)):
letter = chosen_word[position]
if letter = guess:
display[position] = latter
print(display)
#Step 3
import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
#Testing code
print(f'Pssst, the solution is {chosen_word}.')
#Create blanks
display = []
for _ in range(word_length):
display += "_"
#TODO-1: - Use a while loop to let the user guess again. The loop should only stop once the user has guessed all the letters in the chosen_word and 'display' has no more blanks ("_"). Then you can tell the user they've won.
#使用while loop 令使用者猜完空白處
guess = input("Guess a letter: ").lower()
#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
print(display)
#我
while "_" in display:
guess = input("Guess a letter: ").lower()
#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
print(display)
#if "_" not in display:
print("you win")
#老師
end_of_game = False
while not end_of_game
guess = input("Guess a letter: ").lower()
#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
print(display)
if "_" not in display:
end_of_game = True
print("you win")
命名新的變數 lives,主要追蹤遊戲者的生命數
每猜錯一次生命數值要減1
增加吊人圖示
#Step 4 老師
import random
stages = ['''
+---+
| |
O |
/|\\ |
/ \\ |
|
=========
''', '''
+---+
| |
O |
/|\\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
''']
end_of_game = False
word_list = ["ardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
#TODO-1: - Create a variable called 'lives' to keep track of the number of lives left.
#Set 'lives' to equal 6.
lives = 6
#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()
#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
#TODO-2: - If guess is not a letter in the chosen_word,
#Then reduce 'lives' by 1.
#If lives goes down to 0 then the game should stop and it should print "You lose."
if guess not in chosen_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-3: - print the ASCII art from 'stages' that corresponds to the current number of 'lives' the user has remaining.
print(stages[lives])