-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
95 lines (82 loc) · 3.55 KB
/
Hangman.py
File metadata and controls
95 lines (82 loc) · 3.55 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
#Hangman Program
from random import*
wordBank = ["diplomacy", "hairdressing", "inspirer", "electrically", "physics", "art", "career", "freedom", "hello", "elephant", "fearless", "colorado", "computer", "grandfather", "decadent", "command", "moisturiser"]
#############################################
def checkSure(prompt):
choice = input(prompt).lower()
while choice not in ['y','yes',"n",'no']:
choice = input(prompt + "(yes/no)").lower()
if choice in ['n','no']:
return False
else:
return True
############################################
def getGuess(prompt):
while True:
letter = input(prompt)
if len(letter)==1:
return letter.lower()
else:
print("Incorrect Input")
##############################################
def wholeGuess(wordToMatch):
guess = input("Enter the whole word guess: ").lower()
if guess==wordToMatch:
print("Exact word match!\n***You won hangman, Congradulations!***\n")
return True
else:
return False
##############################################
def updateGuessStatus(letterToDisplay, currentDisplay, fullWord):
display = list(currentDisplay)
indexes = []
positionOfCurrentLetter = 0
for currentLetter in fullWord:#get all the positions of a particular letter in a word
if letterToDisplay==currentLetter:
indexes.append(positionOfCurrentLetter)
positionOfCurrentLetter += 1
for letterPosition in indexes:#display the letter at all the posistions found
display[letterPosition] = letterToDisplay
return ''.join(display)
##############################################
#Main code
def hangmanCode():
chosenWord = wordBank[randint(0,len(wordBank)-1)]
guessList = []#to keep all the guesses stored for reference
guessStatus = ""#to display all correct guesses
for item in chosenWord:
guessStatus = guessStatus + "-"
guessesLeft = 12
notWarned = True
print('\n' + guessStatus)
while guessesLeft > 0 and guessStatus!=chosenWord:
#if guessStatus.count("-") > guessesLeft and notWarned:
# notWarned = False
# if checkSure("WARNING\nYou have less guesses left than spaces do you want to start a new game?"):
# return
print("You have " + str(guessesLeft) + " guesses left.")
newLetter = getGuess("Enter a single letter: ")
if newLetter=='!':#option for a whole guess
if wholeGuess(chosenWord):
return
if newLetter in guessList:
print("You have already guessed: " + str(newLetter))
#do not peanalies for repeat guesses of the same letter
else:
guessList.append(newLetter)
guessesLeft -= 1
if newLetter in chosenWord:
guessStatus = updateGuessStatus(newLetter, guessStatus, chosenWord)
print("Correct Guess!")
else:
print("Incorrect Guess!")
print(guessStatus + '\n')
if guessStatus==chosenWord:
print("***You won hangman, Congradulations!***\n")
else:
print("---You lost hangman---\n")
#############################################
print("Welcome to the Hangman Program\n[Type '!' to guess the whole word]")
while checkSure("Do you want to play a game of Hangman?"):
hangmanCode()
print("Exiting Hangman Program")