-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (39 loc) · 1.74 KB
/
main.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import random
from collections import Counter
words = '''apple linux windows python computer gaming programming coding copilot github
vim cpp vue banana orange mango guava grapes strawberry raspberry
blueberry blackberry raspberry watermelon pineapple papaya
engine driveshaft transmission differential suspension
steering brakes wheels tires exhaust intake turbocharger
food drinks snacks breakfast lunch dinner supper'''
words = words.split()
secret_word = random.choice(words)
def display_current_progress(secret_word, guessed_letters):
display = ' '.join([char if char in guessed_letters else '_' for char in secret_word])
print(display)
if __name__ == '__main__':
print('Guess the word!')
display_current_progress(secret_word, [])
attempts_remaining = len(secret_word) + 3
guessed_letters = set()
word_guessed = False
while attempts_remaining > 0 and not word_guessed:
print(f'\nAttempts remaining: {attempts_remaining}')
guess = input('Enter a letter to guess: ').lower()
if not guess.isalpha() or len(guess) != 1:
print('Enter a single letter.')
continue
elif guess in guessed_letters:
print('You have already guessed that letter.')
continue
guessed_letters.add(guess)
if guess in secret_word:
if all(char in guessed_letters for char in secret_word):
word_guessed = True
else:
attempts_remaining -= 1
display_current_progress(secret_word, guessed_letters)
if word_guessed:
print(f'\nCongratulations, You won! The word is: {secret_word}')
else:
print(f'\nYou lost! The word was {secret_word}. Try again.')