Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c85d23

Browse files
committedSep 25, 2021
Hangman game now can be start over after one round
1 parent f49177a commit 3c85d23

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed
 

‎Hangman/hangman.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,71 @@
1-
from hangman_art import logo , stages
1+
from hangman_art import logo, stages
22
from hangman_words import wordlist
33
import random
44

55
# get random word from hangman_words
66
random_word = random.choice(wordlist)
77
word_len = len(random_word)
8+
user_quit = False
89

9-
print(logo)
10-
print('This is your word.Try to guess it!!')
11-
print('Word : ',end =" ")
1210

13-
guess_word = ['_'] * word_len
14-
print(*guess_word)
11+
def is_invalid_input(let):
12+
return let.isdigit() or len(let) != 1
1513

16-
chances = 6
17-
end_of_game = False
1814

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=" ")
2119

22-
flag = 0
23-
letter = input('\n\nGuess a letter: ').lower()
20+
guess_word = ['_'] * word_len
21+
print(*guess_word)
2422

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
2825

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\nGuess a letter: ').lower()
3130

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
3634

37-
if '_' not in guess_word:
38-
end_of_game = True
39-
print('\nCongo!! You won. :)')
40-
break
35+
if letter in guess_word:
36+
print(f"You've already guessed {letter}")
4137

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('\nYou lost :(\nCorrect 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('\nCongo!! You won. :)')
46+
break
5247

48+
if flag == 0:
49+
print(f"You guessed {letter}, that's not in the word. You lose a life")
50+
chances -= 1
5351

52+
print('Guessed Word : ', end=" ")
53+
print(*guess_word)
54+
print(stages[6 - chances])
5455

56+
else:
57+
print('\nYou lost :(\nCorrect word:', random_word)
5558

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

Comments
 (0)
Please sign in to comment.