-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
111 lines (79 loc) · 3.54 KB
/
hangman.py
File metadata and controls
111 lines (79 loc) · 3.54 KB
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
import numpy as np
import random
GAME_OVER = False
ATTEMPTS = 6
def if_lost(ATTEMPTS,GAME_OVER):
if ATTEMPTS == 0:
print('\nThe word was ' + str(word).upper())
print("YOU LOST, BETTER LUCK NEXT TIME!!!")
GAME_OVER = True
return GAME_OVER
def user_input(choice):
change = 0
for a in range(word_len):
if choice.casefold() != word[a]:
pass
else:
hidden_word[a] = choice.casefold()
change += 1
print('\n\n'+' '.join(hidden_word))
return change
def if_won(hidden_word,GAME_OVER):
inc = 0
for r in range(word_len):
if hidden_word[r] != '_':
inc += 1
if inc == word_len:
print("\nCONGRATULATIONS, YOU WON!!")
GAME_OVER = True
return GAME_OVER
ans = input("Type 'y' to start the game : ").casefold()
while ans == 'y':
random_word = np.array(['airplane','angle','Ann','apple','art','Atlantic','atmosphere','bar','barn','baseball','beauty','believed','bell','belong','beneath','bigger','bottle','bowl','broad','chapter','chart','Chinese','clearly','climate','clock','closely','clothing','coffee','cow','cry','Dad','dangerous','deer','desk','detail','development','drew','driver','event','everywhere','fat','favorite','fence','fifty','flight','flow','flower','forget','fourth','friendly','generally','German','Germany','giant','golden','grain','handle','height','hung','hurry','immediately','industry','instance','Italy','James','knife','lake','Latin','','leader','leaving','likely','lunch','mass','master','mile','mix','model','mud','muscle','nearby','nearest','nest','newspaper','nobody','observe','Pacific','peace','plate','plenty','popular','powerful','push','railroad','rapidly','root','rubber','sad','sail','save','score','seeing','serious','service','sheet','shop','silent','smell','smoke','smooth','source','spell','storm','structure','supper','support','sweet','swim','telephone','Texas','threw','throw','tone','tool','track','trail','understanding','upper','view','wagon','western','whatever','wheat','whenever','whom','win','wonderful','wore'])
word = str(random_word[random.randint(0,len(random_word))])
word_len = len(word)
hidden_word = list("_" * word_len)
used = str(' ')
print(" O ")
print(" /|\ ")
print(" | ")
print(" / \ ")
print("\nWELCOME TO HANGMAN!!")
print(' '.join(hidden_word))
while not GAME_OVER:
choice = str(input("\nEnter an alphabet: "))
if user_input(choice) == 0:
ATTEMPTS -= 1
used = used + ' ' + choice
print('words used:' + used)
if if_lost(ATTEMPTS,GAME_OVER):
break
elif if_won(hidden_word,GAME_OVER):
break
if ATTEMPTS == 6:
print(" O ")
print(" /|\ ")
print(" | ")
print(" / \ ")
elif ATTEMPTS == 5:
print(" O ")
print(" /|\ ")
print(" | ")
print(" / ")
elif ATTEMPTS == 4:
print(" O ")
print(" /|\ ")
print(" | ")
elif ATTEMPTS == 3:
print(" O ")
print(" /| ")
print(" | ")
elif ATTEMPTS == 2:
print(" O ")
print(" | ")
print(" | ")
elif ATTEMPTS == 1:
print(" O ")
print("Attempts left: " + str(ATTEMPTS))
ans = input("\n\nWould you like to play again? (y/n) : ").casefold()
print("THANK YOU FOR PLAYING, COME BACK AGAIN")