Checking a Guess
The next step is to check and see if the user’s input was a correct guess. We won’t alter any letters just yet, as we first want to make sure we can identify a correct guess and either output that they guessed correctly or remove a life:
24| game_over = True ◻◻◻
25| elif ans in word: # check if letter in word
26| print("You guessed correctly!")
27| else: # otherwise lose life
28| lives -= 1
29| print("Incorrect, you lost a life.")
Go ahead and run the cell. If you continue to guess incorrectly, you’ll notice the lives will go below zero. Be sure to guess a correct letter and incorrect letter to know that this works.
Clearing Output
Now that we’re getting further with our program, we can see that the loop is continually outputting information below previous outputs. Let’s begin to clear the output:
20| ans = input( ◻◻◻
22| clear_output( ) # clear all previous output
24| if ans == 'quit': ◻◻◻
Go ahead and run the cell. You’ll notice that it properly clears the previous information displayed no matter how long we play. This is a Jupyter Notebook specific function.
Creating the Losing Condition
The next logical operation would be creating a way to lose, since our lives can go below zero:
31| print('Incorrect, ◻◻◻ 33| if lives <= 0:
34| print("You lost all your lives, you lost!")
35| game_over = True
Go ahead and run the cell. Now if you lose all your lives, the game will stop running and tell you that you lost. Remember that the loop is only running until game_over is True, which is what we set it to once the variable of lives drops to zero.
Handling Correct Guesses
Now that we can lose, we need the ability to see correct guesses. To understand how to alter the letters shown, we first need to remember what’s being output. Our guesses list is being turned into a string and output. This means that when the user has guessed correctly, we need to change the items within our guesses list in their corresponding positions. The list is the same length of the word we chose at the beginning of the cell, so each underscore represents a letters position. If the word was “sport”, then the first underscore in “_ _ _ _” would represent the “s”. We simply need to replace the proper underscore in the list with the letter guessed. We can do this by using a for loop and keeping track of the index:
28| print('You guessed correctly!') ◻◻◻
30| # create a loop to change underscore to proper letter 31| for i in range( len(word) ):
32| i f word[ i ] == ans: # comapares values at indexes
33| guesses[ i ] = ans
34| else: ◻◻◻
ChApter 4 LIsts And Loops
Go ahead and run that cell. Now when guessing a correct letter, it will output the change. The for loop is looping to the length of the word, and we’re using the variable “i” to keep track of the index. We then check if each character is equal to the letter guessed.
If it is, then we change the item from an underscore to the letter guessed at that index. Check out Table 4-5 for an example on the process. Let’s use “pop” for our word and “p” for the guess.
Table 4-5. Tacking index value to check guess
Do'stlaringiz bilan baham: |