Skip to content

Commit 165a144

Browse files
added hangman
1 parent e0801ae commit 165a144

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

hangman.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from getpass import getpass
2+
#WARNING: ONE WORD ONLY, will add multiple words in a later update maybe
3+
#lol this is just to check my python skills on classes
4+
class Hangman:
5+
def __init__(self,input1):
6+
self.input1 = input1
7+
def play(self):
8+
def remove(a,li):
9+
return [val for val in li if val!=a]
10+
def check_win(li, input1):
11+
return ''.join(li)==input1
12+
if len(self.input1.split())>1:
13+
print("RASCAL")
14+
else:
15+
li = [letter for letter in self.input1]
16+
lia = ['_' for letter in self.input1]
17+
for each in lia:
18+
print(each,end=" ")
19+
print()
20+
for j in range(len(self.input1)):
21+
print(j+1, end=" ")
22+
print()
23+
count = 7
24+
#main game loop
25+
while count!=0:
26+
st = ""
27+
a = input("Enter your one letter guess: ")
28+
if a in li:
29+
li = remove(a,li)
30+
for i in range(len(self.input1)):
31+
if self.input1[i] not in li:
32+
lia[i] = self.input1[i]
33+
for z in range(len(lia)):
34+
print(lia[z],end=" ")
35+
print()
36+
for j in range(len(self.input1)):
37+
print((j+1), end=" ")
38+
print()
39+
if check_win(lia,self.input1)==True:
40+
break
41+
else:
42+
print("Sorry, letter doesn't fit!")
43+
count-=1
44+
if count==0:
45+
print("Sorry, you lost")
46+
else:
47+
print("Nice, you got the word")
48+
49+
input1 = getpass()
50+
c = Hangman(input1)
51+
c.play()

0 commit comments

Comments
 (0)