def find_positions(letter, word):
"""Returns a list of positions where the letter appears in the word.
Parameters
----------
letter : str
A single character string representing the guessed letter.
word : str
A string representing the chosen word in which to find occurrences of the letter.
Returns
-------
list of int
A list of positions (indices) where the letter appears in the word.
"""
print(find_positions('a', 'hangman'))
# Output: [1, 5]
print(find_positions('o', 'computer'))
# Output: [1]
print(find_positions('z', 'python'))
# Output: []
Lab 6 will be focused on functions abd modules. We will use modules such as matlplotlib, random, and time to help with building a hangman game!
Purpose and Outcomes
We will practice:
-
declaring, defining, and changing variables
-
implementing flow control through while/for loops and if statements,
-
adhering to function contract information,
-
manipulating lists, and
-
graphing using matlib plot
By the end of this lab, you should be able to:
-
correctly append, delete and extract information in a list,
-
use basic functionality from matlibplot module, and
-
understand why import statements are important in a script
Preparation
In lab 6, we will use the following sensors:
-
Buzzer (to indicate a wrong guess)
-
LED (to indicate a correct guess)
-
OLED screen
-
Button (to allow the user to play another round)
Here are the rules/specifications of the game:
-
The player has 6 chances only to guess the word
-
The player will guess letter by letter (cannot provide a complete word)
-
The name of the player will be entered once the game is started. Then, the player is asked to choose a difficulty level (easy, medium, hard)
-
Based on the choice, a random word from the corresponding choice is chosen
-
A statement that shows how many letters in the word will be displayed to the player.
-
The player will start guessing letter by letter as long as they still have chances left to play
-
If the guess is more than one letter, or if the guess is not a letter, it will be counted as an invalid input. It will not be counted as a missed chance will be allowed to guess again.
-
If the player correctly guessed that letter earlier, it will not be counted as a missed chance. A message that says "You already guessed that letter. Try again!" will be displayed.
-
If the guess is correct, the letter and its position will be displayed to the user (Question 2 of the pre-lab)
-
If the guess is incorrect, one chance is taken from the player. At the end of each guess, the progress is summarized to the player, showing how many chances are left and the current guessed word.
-
If the player runs out of chances, the game should end and the message "Game over! The word was: _"_ should be displayed to the user (indicating the word).
-
The player will be given 5 seconds to press the button if another round is desired. Otherwise, the game will not be restarted
Having discussed the specifications of the game, let’s get to the design!
-
Design: Define a function called find_positions(letter, word). The function should take two arguments:
-
letter
: A single character string. -
word
: A string representing the chosen word. -
The function should return a list containing the positions (indices) where the letter appears in word.
-
Example Usage:
illustration example:
-
-
Functions: familiarize yourself with the following functions. Feel free to look them up. Note in your logbook what would be the output of the following functions and how to use them:
-
random.choice(ListOfWords)
-
letter.isalpha()
-
' '.join(ListOfLetters)
-
Warning
|
Code submission: Remember to label the code with the corresponding step according to the procedure section. |
Procedure
Download Lab6_w25.py and open it within Thonny.
2.1 Define the function def find_positions() from your pre-lab:
Refer to your pre-lab and the function description in the python script to implement and test the function.
2.2 Part I of the hangman game
Implement the following steps inside the while loop:
-
Ask for the player’s name
-
Then, ask the user to choose a difficulty level: [0] - easy, [1] - medium, [2] - hard
-
You can assume that the user will only enter integer numbers (i.e.,
0
,1
, or2
)
-
-
Based on the choice, choose a random word from the corresponding choice
-
Display a statement that shows how many letters are in the word to the player.
-
Define pos_list, which is a list that displays the current guess situation. It starts where all the letters are displayed as "_".
-
It looks like the following:
-

Tip
|
Note that the number of letters is varied based on the random choice (The implementation should be general!). Therefore, pos_list size will depend on the chosen word. |
2.3 Part II of the hangman game
-
Implement the loop that allows continuing the guessing as long as there are chances left to play
-
Take a guess from the user
-
Implement the following based on the guess:
-
If the guess is more than one letter, or if the guess is not a letter, it will be counted as an invalid input. It will not be counted as a missed chance. A message that says "Invalid Input" will be displayed. The player will guess again.
-
If the player already guessed a correct letter earlier, it will not be counted as a missed chance. A message that says "You already guessed that letter. Try again!" will be displayed.
-
If the guess is correct, the letter and its position will be displayed to the user.
-
You can get the positions by calling your function from Step 2.1
-
pos_list will be updated based on the obtained positions and displayed to the user (showing the current progress).
-
LED should turn on.
-
guessed_word will be updated as well. It will look like the following:
-
-

-
If the guess is incorrect, one chance is taken from the player, and the hangman picture will be displayed (the correct level of hangman pic should be displayed). The buzzer will sound as well.
-
If the player finally guessed the whole word correctly, a message that says "Congratulations!" will be displayed to the user along with the full-guessed word. The round will end.
-
At the end of each guess, the progress is summarized to the player, showing how many chances are left and the current guessed word, which is pos_list.
Tip
|
Note: it is not necessarily that all the listed will be standalone "if" conditions. There should be an if-elif-else flow control. The listed above is just what is expected in each case. Think of how you can design the implementation. |
2.4 Part III of the hangman game
-
If the player ran out of chances (the loop terminated because chances = 0). A message that says, "Game over! The word was: _ "_ will be displayed to the user, showing the actual word.
-
Add the name of the player and their corresponding score to History_n and History_s.
-
A message will be displayed to ask the user to press the button if they want to play again.
-
Implement a sleep of a second or two to prepare If the button is not pressed, end the game. If it is pressed, a new player will enter the information and play the game
2.5 Plot a summary of the results
If the game ended, we would like to see who played the game and how many chances they had, which represents their score.
Tip
|
Matplotlib is "a Python 2D plotting library which produces publication quality figures. For simple plotting the pyplot module from matplotlib provides a MATLAB-like interface (MATLAB is another scripting language you will become quite familiar with during Engineering). We will use pyplot module to plot our collected data." To use it, you need to import the library as: import matplotlib.pyplot as plt |
-
Use Matplotlib functionality within your main script to plot History.
-
Use a bar plot where the x-axis is the names of the players, and the y-axis is the number of chances left. It will look something like the following if only two players played:

Q1: Write the code for all the implementation steps. Label each block of code by the corresponding step number.
Q2: Include a screenshot of a sample plot you created using your code.
Tip
|
You can refer to the pyplot tutorial for information on how to plot your data. It can be useful for your project as well! |
Testing
Include the testing results in a tabular format. Include all test cases and the corresponding output. Investigate and write in the comments section what you modified to achieve the expected result.