-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletter_guessing_game.py
More file actions
27 lines (22 loc) · 997 Bytes
/
Copy pathletter_guessing_game.py
File metadata and controls
27 lines (22 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def letter_guessing_game():
secret_word = "python"
guessed_letters = []
attempts = 6
print("Welcome to Letter Guessing Game!")
print(f"The word has {len(secret_word)} letters.")
while attempts > 0 and not all(letter in guessed_letters for letter in secret_word):
print("Word: " + ''.join(letter if letter in guessed_letters else '_' for letter in secret_word))
letter = input("Guess a letter: ").lower()
if letter in guessed_letters:
print(f"You've already guessed the letter '{letter}'")
elif letter in secret_word:
print("Correct guess!")
guessed_letters.append(letter)
else:
print("Incorrect guess!")
attempts -= 1
if all(letter in guessed_letters for letter in secret_word):
print(f"Congratulations! You guessed the word: {secret_word}")
else:
print(f"Sorry, you're out of attempts! The word was: {secret_word}")
letter_guessing_game()