Skip to content

Commit acf7acd

Browse files
Create hangman_game.md
This Hangman game script is a simple Python program designed to let players guess movie titles. It starts by importing the random module to select a movie from a predefined list. The game displays the movie title as underscores and reveals correctly guessed letters. Players have six attempts to guess the entire title, entering one letter at a time. The script checks if the input is valid, updates the list of guessed letters, and adjusts the number of attempts based on the correctness of the guess. The game continues until the player either guesses the title correctly or runs out of attempts. Upon completion, it congratulates the player for a correct guess or reveals the movie title if the attempts are exhausted. The main execution block ensures the game runs only when the script is executed directly.
1 parent c56bbb3 commit acf7acd

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

contrib/mini-projects/hangman_game.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# Hangman - Movies Edition
2+
The Hangman game script is a simple Python program designed to let players guess movie titles. It starts by importing the random module to select a movie from a predefined list. The game displays the movie title as underscores and reveals correctly guessed letters. Players have six attempts to guess the entire title, entering one letter at a time. The script checks if the input is valid, updates the list of guessed letters, and adjusts the number of attempts based on the correctness of the guess. The game continues until the player either guesses the title correctly or runs out of attempts. Upon completion, it congratulates the player for a correct guess or reveals the movie title if the attempts are exhausted. The main execution block ensures the game runs only when the script is executed directly.Below is first the code and then an explanation of the code and its components.
3+
4+
## Code
5+
6+
```
7+
import random
8+
9+
def choose_movie():
10+
movies = ['avatar', 'titanic', 'inception', 'jurassicpark', 'thegodfather', 'forrestgump', 'interstellar', 'pulpfiction', 'shawshank']
11+
return random.choice(movies)
12+
13+
def display_word(movie, guessed_letters):
14+
display = ""
15+
for letter in movie:
16+
if letter in guessed_letters:
17+
display += letter + " "
18+
else:
19+
display += "_ "
20+
return display
21+
22+
def hangman_movies():
23+
movie = choose_movie()
24+
guessed_letters = []
25+
attempts = 6
26+
27+
print("Welcome to Hangman - Movies Edition!")
28+
print("Try to guess the name of the movie. You have 6 attempts.")
29+
30+
while attempts > 0:
31+
print("\n" + display_word(movie, guessed_letters))
32+
guess = input("Guess a letter: ").lower()
33+
34+
if len(guess) != 1 or not guess.isalpha():
35+
print("Please enter a single letter.")
36+
continue
37+
38+
if guess in guessed_letters:
39+
print("You've already guessed that letter.")
40+
continue
41+
42+
guessed_letters.append(guess)
43+
44+
if guess not in movie:
45+
attempts -= 1
46+
print(f"Sorry, '{guess}' is not in the movie name. You have {attempts} attempts left.")
47+
else:
48+
print(f"Good guess! '{guess}' is in the movie name.")
49+
50+
if "_" not in display_word(movie, guessed_letters):
51+
print(f"\nCongratulations! You guessed the movie '{movie.capitalize()}' correctly!")
52+
break
53+
54+
if attempts == 0:
55+
print(f"\nSorry, you ran out of attempts. The movie was '{movie.capitalize()}'.")
56+
57+
if __name__ == "__main__":
58+
hangman_movies()
59+
```
60+
61+
## Code Explanation
62+
63+
### Importing the Random Module
64+
65+
```python
66+
67+
import random
68+
69+
```
70+
71+
The `random` module is imported to use the `choice` function, which will help in selecting a random movie from a predefined list.
72+
73+
### Choosing a Movie
74+
75+
```python
76+
77+
def choose_movie():
78+
79+
movies = ['avatar', 'titanic', 'inception', 'jurassicpark', 'thegodfather', 'forrestgump', 'interstellar', 'pulpfiction', 'shawshank']
80+
81+
return random.choice(movies)
82+
83+
```
84+
85+
The `choose_movie` function returns a random movie title from the `movies` list.
86+
87+
### Displaying the Word
88+
89+
```python
90+
91+
def display_word(movie, guessed_letters):
92+
93+
display = ""
94+
95+
for letter in movie:
96+
97+
if letter in guessed_letters:
98+
99+
display += letter + " "
100+
101+
else:
102+
103+
display += "_ "
104+
105+
return display
106+
107+
```
108+
109+
The `display_word` function takes the movie title and a list of guessed letters as arguments. It constructs a string where correctly guessed letters are shown in their positions, and unknown letters are represented by underscores (`_`).
110+
111+
### Hangman Game Logic
112+
113+
```python
114+
115+
def hangman_movies():
116+
117+
movie = choose_movie()
118+
119+
guessed_letters = []
120+
121+
attempts = 6
122+
123+
print("Welcome to Hangman - Movies Edition!")
124+
125+
print("Try to guess the name of the movie. You have 6 attempts.")
126+
127+
while attempts > 0:
128+
129+
print("\n" + display_word(movie, guessed_letters))
130+
131+
guess = input("Guess a letter: ").lower()
132+
133+
if len(guess) != 1 or not guess.isalpha():
134+
135+
print("Please enter a single letter.")
136+
137+
continue
138+
139+
if guess in guessed_letters:
140+
141+
print("You've already guessed that letter.")
142+
143+
continue
144+
145+
guessed_letters.append(guess)
146+
147+
if guess not in movie:
148+
149+
attempts -= 1
150+
151+
print(f"Sorry, '{guess}' is not in the movie name. You have {attempts} attempts left.")
152+
153+
else:
154+
155+
print(f"Good guess! '{guess}' is in the movie name.")
156+
157+
if "_" not in display_word(movie, guessed_letters):
158+
159+
print(f"\nCongratulations! You guessed the movie '{movie.capitalize()}' correctly!")
160+
161+
break
162+
163+
if attempts == 0:
164+
165+
print(f"\nSorry, you ran out of attempts. The movie was '{movie.capitalize()}'.")
166+
167+
```
168+
169+
The `hangman_movies` function manages the game's flow:
170+
171+
1. It selects a random movie title using `choose_movie`.
172+
173+
2. Initializes an empty list `guessed_letters` and sets the number of attempts to 6.
174+
175+
3. Prints a welcome message and the initial game state.
176+
177+
4. Enters a loop that continues until the player runs out of attempts or guesses the movie title.
178+
179+
5. Displays the current state of the movie title with guessed letters revealed.
180+
181+
6. Prompts the player to guess a letter.
182+
183+
7. Validates the player's input:
184+
185+
- Ensures it is a single alphabetic character.
186+
187+
- Checks if the letter has already been guessed.
188+
189+
8. Adds the guessed letter to `guessed_letters`.
190+
191+
9. Updates the number of attempts if the guessed letter is not in the movie title.
192+
193+
10. Congratulates the player if they guess the movie correctly.
194+
195+
11. Informs the player of the correct movie title if they run out of attempts.
196+
197+
### Main Execution Block
198+
199+
```python
200+
201+
if __name__ == "__main__":
202+
203+
hangman_movies()
204+
205+
```
206+
## Conclusion
207+
This block ensures that the game runs only when the script is executed directly, not when it is imported as a module.
208+
209+
## Output Screenshots:
210+
211+
![image](https://github.com/Aditi22Bansal/learn-python/assets/142652964/a7af1f7e-c80e-4f83-b1f7-c7c5c72158b4)
212+
![image](https://github.com/Aditi22Bansal/learn-python/assets/142652964/082e54dc-ce68-48fd-85da-3252d7629df8)
213+
214+
215+
216+
## Conclusion
217+
218+
This script provides a simple yet entertaining Hangman game focused on guessing movie titles. It demonstrates the use of functions, loops, conditionals, and user input handling in Python.
219+
220+

0 commit comments

Comments
 (0)