forked from codesopenly/Hangman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman..py
69 lines (56 loc) · 2.12 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
# Import the python library called random to generate random words
import random
# 4 words are saved in an array list
word2 = random.choice(['python', 'java', 'kotlin', 'javascript'])
#The selected random word will be saved in the word3 variable
word3 = word2
#This will put the word as a list
word3 = list(word3)
#This is a for loop to iterate thorugh all the index in the word3 variable and print a dash
for i in range(len(word2)):
word3[i] = '-'
word3 = ''.join([str(elem) for elem in word3])
print("H A N G M A N")
input1 = []
i = 0
# This is a while loop
while i < 8:
word3 = ''.join([str(elem) for elem in word3])
print(f'\n{word3}', end = ' ')
if '-' in word3:
#This will take an input from the user by displaying the sentence in inverted comma
word = input('\nInput a letter: ')
if len(word) != 1 or word == ' ':
print('You should input a single letter')
elif word.isalpha() and word.isupper():
print('It is not an ASCII lowercase letter')
elif not word.isalpha():
print('It is not an ASCII lowercase letter')
else:
if word not in input1:
if word not in word3:
input1.extend(word)
word2 = list(word2)
word3 = list(word3)
#**************************************************************************#
if word in word2:
for j in range(len(word2)):
if word2[j] == word:
word3[j] = word
word3 = ''.join([str(elem) for elem in word3])
else:
print('No such letter in the word')
i +=1
else:
print('You already typed this letter')
i += 1
else:
print('You already typed this letter')
else:
break
word2 = ''.join([str(elem) for elem in word2])
if word3 == word2:
print("""\nYou guessed the word!
You survived!""")
else:
print('You are hanged!')