-
Notifications
You must be signed in to change notification settings - Fork 0
/
HangsmanGame.py
64 lines (51 loc) · 1.47 KB
/
HangsmanGame.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
#The old good Hangsman game
#The App choose a word randomly from thw wordlist.txt
#the user have by def 15 lives
import random
from os import system, name
#choosing a word
with open('wordlist.txt', 'r' , encoding = "utf8") as f:
word=random.choice(f.readlines()).rstrip()
# Init vars
found=False
lives=15
tries=0;
letters=[]
#
def clear():
# check and make call for specific operating system
# for mac and linux(here, os.name is 'posix')
if name == 'posix':
_ = system('clear')
# for win
else:
_ = system('cls')
def hide(): # hide not found yet letters function -
hidden=''
for letter in word:
if letter in letters:
hidden += letter
else:
hidden += '-'
return hidden
def misscounter(tries,lifes): # count misses
found=False
for letter in word:
if letter in letters:
found = True
tries +=1
print (f'Your have used {tries} lives of your {lives}')
return tries
letter= input('Please give a letter! or press - to exit')
letters.append(letter)
hidden_word=hide()
while (letter != '-' and tries<lives ): #hide letters
tries=misscounter(tries,lives)
print (hidden_word)
hidden_word=hide()
clear()
letter= input('Please give a letter! or press - to exit!')
if (letter=='-'):
print ('bye!')
else:
print ('Sorry you lost! You can try again! bye!')