Repository navigation
Expand file tree
/
Copy pathhangman.py
More file actions
153 lines (127 loc) · 2.18 KB
/
Copy pathhangman.py
File metadata and controls
153 lines (127 loc) · 2.18 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random
obrazky = [
"""~~~~~~~""",
"""+
|
|
|
|
|
~~~~~~~""",
"""
+---.
|
|
|
|
|
~~~~~~~ """,
"""
+---.
| |
|
|
|
|
~~~~~~~""",
"""
+---.
| |
| O
|
|
|
~~~~~~~""",
"""
+---.
| |
| O
| |
|
|
~~~~~~~""",
"""
+---.
| |
| O
| --|
|
|
~~~~~~~""",
"""
+---.
| |
| O
| --|--
|
|
~~~~~~~""",
"""
+---.
| |
| O
| --|--
| /
|
~~~~~~~""",
"""
+---.
| |
| O
| --|--
| / \
|
~~~~~~~"""]
def find_all(letter,field):
indices = [i for i, x in enumerate(field) if x == letter]
return indices
# define a function for generating a random word
def choose_word():
words = ['hackathon']
return random.choice(words)
word = choose_word()
field = '-'*len(word)
score = 0
# main loop
while True:
# print out current state of the game
# for example - We're guessing the word: _ _ _ _ _ _ _ _ _ _
print(field)
# let's get a letter from the user
while True:
try:
letter = str(input("Guess the letter: "))
except ValueError:
print('Choose letter')
# make sure the letter is only one character
if len(letter) == 1:
break
# print the letter to see if everything is ok by now
print("You guessed", letter)
print(field)
# determine if the letter is in the word
if letter not in word:
score += 1
print(obrazky[score])
if score == len(obrazky):
print('You loose!')
else:
field = ''.split(field)
for i in range(len(field)):
if word[i] == letter:
field[i] = letter
else:
pass
field = ''.join(field)
# for i in range(len(word)):
# if word[i] == letter:
# field = field[:i]+letter+field[i:]
# else:
# pass
# field = [letter if i==letter else '-' for i in word]
# field = ''.join(field)
# indices = find_all(letter,field)
# for i in indices:
# field[indices[i]] = letter
# print(field)
# check if the word is not guessed in its entirety