-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
executable file
·63 lines (46 loc) · 1.4 KB
/
hangman.py
File metadata and controls
executable file
·63 lines (46 loc) · 1.4 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
#!/usr/bin/env python3
word = "Potsdam" #the word to be guessed
triesLeft = 8 #the total number of wrong guess you have
guesses = [] #a list of all guesses made
found = False #Did you find the whole word or not
while(triesLeft >= 0 and not found):
#print text to the screen
print("\n\nWe are playing hangman !")
print("You have " + str(triesLeft) + " tries left!")
print("Letters you tried so far :")
print(guesses)
#print word so far
output = []
for letter in word:
if (letter.lower() in guesses):
output.append(letter)
else:
output.append("_")
print("\nThe word we are looking for:")
print(" ".join(output))
print("")
#get the next guess from the user
guess = input("What is your next guess? ")
if (len(guess) == 1): #is the input valid ?
guess = guess.lower()
if not (guess in guesses): #has this character been guessed before ?
guesses.append(guess)
if (guess in word.lower()): #is is a correct guess?
print("Correct!")
#check if the word was found
f = True
for letter in word:
f = f and (letter.lower() in guesses)
found = f
else:
print("Wrong Guess!")
triesLeft -= 1
else:
print("You already tried this character!")
else:
print("Incorrect input, you can only guess one character!")
if not found:
print("\n\nYou Lost!")
print("We were looking for " + word)
else:
print("\n\nCongratulations, you guessed the word!")