-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
105 lines (99 loc) · 1.81 KB
/
hangman.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
stages = [
'''
+---+
| |
O |
/|\ |
/ \ |
|
=========
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
''', '''
+---+
| |
O |
/|\ |
|
|
=========
''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========
''', '''
+---+
| |
O |
|
|
|
=========
''', '''
+---+
| |
|
|
|
|
=========
'''
]
lives = 6
Game_over = False
#step 1 :- Generate a random word
import random
word_list = [
'HyperTextMarkupLink', 'CascadingStyleSheets', 'Python', 'Java', 'JavaScript', 'Swift', 'C++', 'C#', 'R',
'Golang', 'PHP', 'TypeScript', 'Scala', 'Shell', 'PowerShell', 'Perl',
'Haskell', 'Kotlin', 'VisualBasicNET', 'StructuredQueryLanguage', 'Delphi',
'MATLAB', 'Groovy', 'Lua', 'Rust', 'Ruby'
]
chosen_word = random.choice(word_list).lower()
print(chosen_word)
#Step 2:- Generate blanks
display = list()
for i in range(0, len(chosen_word)):
display.append('_')
#print(' '.join(display))
while not Game_over:
#Step 3:- Ask user to guess a letter
guess = input("Select a letter \n")
#Step 4:-Is the guessed letter in the word_list
for position in range(0, len(display)):
if guess == chosen_word[position]:
#Step 5:-Replace the blank with the letter
display[position] = guess
#Step 6:-Lose a life
if guess not in chosen_word:
lives -= 1
#Step 7:- Have they run out of lives
if lives == 0:
print("you lose")
print()
Game_over = True
print(' '.join(display))
#Step 8:- Are all the blanks filled
if "_" not in display:
print("You Win")
Game_over = True
print(stages[lives])
#Step 9:- Game Over
print("Game over")