File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ import random ;
2
+ ALLOWED_FAILURES = 6 ;
3
+ words = ["buffalo" , "parrot" , "squirrel" , "hedgehog" ];
4
+ word = random .choice (words );
5
+
6
+ correct_letters = ['_' ] * len (word );
7
+ incorrect_letters = [];
8
+
9
+ letters_remaining = len (word );
10
+ while letters_remaining > 0 and len (incorrect_letters ) < ALLOWED_FAILURES :
11
+ guess = input ("Guess a letter: " ).lower ();
12
+ if guess in correct_letters or guess in incorrect_letters :
13
+ print ("That letter has already been used." );
14
+ continue ;
15
+ elif not guess .isalpha ():
16
+ print ("That is not a letter." );
17
+ continue ;
18
+
19
+ i = 0 ;
20
+ is_in_word = False ;
21
+ for letter in word :
22
+ if guess == letter :
23
+ is_in_word = True ;
24
+ correct_letters [i ] = letter ;
25
+ letters_remaining -= 1 ;
26
+ i += 1 ;
27
+
28
+ if not is_in_word :
29
+ incorrect_letters .append (guess );
30
+
31
+ print (f"The word so far: { correct_letters } " );
32
+ print (f"Incorrect guesses: { incorrect_letters } " );
33
+
34
+ print ("Game Over" );
35
+ if len (incorrect_letters ) < ALLOWED_FAILURES :
36
+ print ("Player wins!" );
37
+ else :
38
+ print ("Player loses :(" );
You can’t perform that action at this time.
0 commit comments