1
- from hangman_art import logo , stages
1
+ from hangman_art import logo , stages
2
2
from hangman_words import wordlist
3
3
import random
4
4
5
5
# get random word from hangman_words
6
6
random_word = random .choice (wordlist )
7
7
word_len = len (random_word )
8
+ user_quit = False
8
9
9
- print (logo )
10
- print ('This is your word.Try to guess it!!' )
11
- print ('Word : ' ,end = " " )
12
10
13
- guess_word = [ '_' ] * word_len
14
- print ( * guess_word )
11
+ def is_invalid_input ( let ):
12
+ return let . isdigit () or len ( let ) != 1
15
13
16
- chances = 6
17
- end_of_game = False
18
14
19
- # game logic
20
- while not end_of_game and chances != 0 :
15
+ while not user_quit :
16
+ print (logo )
17
+ print ('This is your word.Try to guess it!!' )
18
+ print ('Word : ' , end = " " )
21
19
22
- flag = 0
23
- letter = input ( ' \n \n Guess a letter: ' ). lower ( )
20
+ guess_word = [ '_' ] * word_len
21
+ print ( * guess_word )
24
22
25
- if (letter .isdigit () or len (letter ) != 1 ):
26
- print ('Please enter single letter (Numbers not allowed)' )
27
- continue
23
+ chances = 6
24
+ end_of_game = False
28
25
29
- if letter in guess_word :
30
- print (f"You've already guessed { letter } " )
26
+ # game logic
27
+ while not end_of_game and chances != 0 :
28
+ flag = 0
29
+ letter = input ('\n \n Guess a letter: ' ).lower ()
31
30
32
- for i in range (word_len ):
33
- if random_word [i ] == letter :
34
- guess_word [i ] = letter
35
- flag = 1
31
+ if is_invalid_input (letter ):
32
+ print ('Please enter single letter (Numbers not allowed)' )
33
+ continue
36
34
37
- if '_' not in guess_word :
38
- end_of_game = True
39
- print ('\n Congo!! You won. :)' )
40
- break
35
+ if letter in guess_word :
36
+ print (f"You've already guessed { letter } " )
41
37
42
- if flag == 0 :
43
- print (f"You guessed { letter } , that's not in the word. You lose a life" )
44
- chances -= 1
45
-
46
- print ('Guessed Word : ' ,end = " " )
47
- print (* guess_word )
48
- print (stages [6 - chances ])
49
-
50
- else :
51
- print ('\n You lost :(\n Correct word:' ,random_word )
38
+ for i in range (word_len ):
39
+ if random_word [i ] == letter :
40
+ guess_word [i ] = letter
41
+ flag = 1
42
+
43
+ if '_' not in guess_word :
44
+ end_of_game = True
45
+ print ('\n Congo!! You won. :)' )
46
+ break
52
47
48
+ if flag == 0 :
49
+ print (f"You guessed { letter } , that's not in the word. You lose a life" )
50
+ chances -= 1
53
51
52
+ print ('Guessed Word : ' , end = " " )
53
+ print (* guess_word )
54
+ print (stages [6 - chances ])
54
55
56
+ else :
57
+ print ('\n You lost :(\n Correct word:' , random_word )
55
58
59
+ while not user_quit :
60
+ user_input = input ('Do you want to play again? (y/n)' )
61
+ if not is_invalid_input (user_input ):
62
+ user_input = user_input .strip ().lower ()
63
+ if user_input == 'y' :
64
+ break
65
+ elif user_input == 'n' :
66
+ user_quit = True
67
+ print ('Goodbye!' )
68
+ else :
69
+ print ('wrong input, please enter y or n' )
70
+ else :
71
+ print ('wrong input, please enter y or n' )
0 commit comments