-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_the_word.py
54 lines (38 loc) · 1.75 KB
/
guess_the_word.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
50
51
52
53
54
# Simple guess the word game:
import random
words_to_guess = ["pineapple", "pear", "banana", "grape", "kiwi", "lemon", "orange", "apple", 'grapefruit', 'raspberry',
'peach', 'melon', 'strawberry', 'watermelon', 'blueberry', 'cherry', 'blackberry', 'coconut', 'plum']
def start_game():
start = input("If you would like to start game press 'y' or quit then press 'q': ").lower()
if start == 'y':
tries = 10
player_name = input('Enter your name: ')
random_word = list(random.choice(words_to_guess))
display_word = ['_ '] * len(random_word)
print('\nYou have 12 tries to guess the word. Each incorrect letter entered means one less try.')
print(f'Good luck {player_name}!')
print(f'Your word to guess has {len(random_word)} letters (category: fruits).\n')
print('_ ' * len(random_word))
while tries > 0 and random_word != display_word:
guess_letter = input("\nEnter letter to guess the word: ")
if guess_letter in random_word:
for index, letter in enumerate(random_word):
if letter == guess_letter:
display_word[index] = letter
else:
tries -= 1
print(' '.join(display_word))
print(f'\nThe remaining tries: {tries}')
if random_word == display_word:
print(f'Congrats {player_name}! You won. Your guessed word is: {"".join(display_word)}.')
if tries == 0:
print("You've lost!")
quit()
elif start == 'q':
print('Bye bye then!')
quit()
else:
print('Unknown command, please try again: ')
start_game()
if __name__ == '__main__':
start_game()