forked from skywalker290/hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
124 lines (110 loc) · 3.59 KB
/
4.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import random
print("Welcome to Hangman!")
name = input("Enter your nickname: ")
print("Hello " + name + "!")
print("Let's play Hangman!")
def main():
global count
global display
global word
global guessed
global length
global play
words_to_guess = ["coding", "python", "data", "machine", "chocolate", "laptop", "story", "library", "country", "civilsation"]
word = random.choice(words_to_guess)
length = len(word)
count = 0
display = '_ ' * length
guessed = []
play = ""
def play_loop():
global play
play = input("Do you want to play again? (y/n) \n")
while play not in ["y", "n","Y","N"]:
play = input("Do you want to play again? (y/n) \n")
if play == "y" or play == "Y" :
main()
elif play == "n" or play == "N":
print("Thanks For Playing! We expect you back again!")
exit()
def hangman():
global count
global display
global word
global guessed
global play
limit = 5
guess = input("This is the word to be guessed: " + display + " Enter your guessing letter: \n")
guess = guess.strip()
if len(guess.strip()) == 0 or len(guess.strip()) > 1 or guess in [0,1,2,3,4,5,6,7,8,9]:
print("Invalid Input. Please type in only one letter.")
hangman()
elif guess in word:
guessed.extend([guess])
index = word.find(guess)
word = word[:index] + "_ " + word[index + 1:]
display = display[:index] + guess + display[index + 1:]
print(display + "\n")
elif guess in guessed:
print("Try another letter.\n")
else:
count += 1
if count == 1:
print(" _____ \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 2:
print(" _____ \n"
" | | \n"
" | |\n"
" | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 3:
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " guesses remaining\n")
elif count == 4:
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | \n"
" | \n"
"__|__\n")
print("Wrong guess. " + str(limit - count) + " last guess remaining\n")
elif count == 5:
print(" _____ \n"
" | | \n"
" | |\n"
" | | \n"
" | O \n"
" | /|\ \n"
" | / \ \n"
"__|__\n")
print("You couldn't guess the word! Game over!")
print("The word was:",guessed,word)
play_loop()
if word == '_ ' * length:
print("Congratulations! You have guessed the word correctly!")
play_loop()
elif count != limit:
hangman()
main()
hangman()