Adding Guessed Letters
Let’s now add the functionality to append the user’s guess to our guesses list:
37| print("Incorrect, ◻◻◻ 39| if ans not in guessed:
40|| guessed.append(ans) # add ans to guessed list
42| if lives <= 0: ◻◻◻
Go ahead and run the cell. Now the guesses list will update as the user plays the game.
ChApter 4 LIsts And Loops
Handling Previous Guesses
The very last order of business is making sure that when they guess the same letter again, that they don’t have a life taken away, but rather they are alerted that it’s been guessed. We’ll need to rewrite the entire conditional statement for checking if the letter is in the word though:
27|| game_over = True ◻◻◻
28| elif ans in word and ans not in guessed:
29|| print("You guessed correctly!") ◻◻◻
34| guesses[ i ] = ans ◻◻◻ 35| elif ans in guessed:
36|| print("You already guessed that. Try again.")
37| else: ◻◻◻
Go ahead and run the cell. We had to change the elif statement for line 28 because we also needed to check that the letter was not added to the guessed list yet. On line 35 we add a second elif statement that will check if the letter is specifically in the guessed list. Remember that once an if/elif statement runs, the following statements will not. If neither of those conditionals are True, then it means that they haven’t guessed the letter yet, and it’s not in the random word. The game is now complete with full functionality.
Final Output
Congratulations on completing this project! Due to the size of the project, the full code will not be written here. Instead, you may find the completed version of the code where this book’s resource files are located on Github. You can find the link in the front of the book. All resource files for each week are located within that link. To find the specific code for this project, simply open or download the “Week_04.ipynb” file. If you ran into errors along the way, be sure to cross-reference your code with the code in this file and see where you may have gone wrong. The final code output for all future projects can also be found in the same location, so be sure to bookmark this page.
What a day! We were able to use the concept of looping, along with the power of lists to create a fun game. try adding your own flare, or breaking it, to understand further what may or may not work.
Weekly Summary
This was certainly one of the longer weeks, filled with a ton of information. Be sure to take some time to practice these concepts, either on your own or by completing the end of week exercises. We covered why lists are so important in Python and how to use them within our program. Also covered were the two loops that Python offers, for loops and while loops. Using loops, we can rerun code as many times as necessary, or to iterate over data collections like lists. If you feel overwhelmed with all the information, rest assured that we use loops and lists in everything that we do. This will give you a lot of practice and repetition.
Do'stlaringiz bilan baham: |