Created
October 3, 2020 20:51
-
-
Save zerodeaths434/0ff2c56f44436edcac6fb1052bb46098 to your computer and use it in GitHub Desktop.
hey man
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import json | |
HANGMAN_PICS = [''' | |
+---+ | |
| | |
| | |
| | |
===''', ''' | |
+---+ | |
O | | |
| | |
| | |
===''', ''' | |
+---+ | |
O | | |
| | | |
| | |
===''', ''' | |
+---+ | |
O | | |
/| | | |
| | |
===''', ''' | |
+---+ | |
O | | |
/|\ | | |
| | |
===''', ''' | |
+---+ | |
O | | |
/|\ | | |
/ | | |
===''', ''' | |
+---+ | |
O | | |
/|\ | | |
/ \ | | |
==='''] | |
def getRandomWord(): | |
list = [] | |
for keys in data.keys(): | |
list.append(keys) | |
f.close() | |
num=random.randint(0,len(list)) | |
k = list[num].split() | |
return(k[0]) | |
def displayBoard(missedLetters, correctLetters, secretWord): | |
print(HANGMAN_PICS[len(missedLetters)]) | |
print() | |
print('Missed letters:', end=' ') | |
for letter in missedLetters: | |
print(letter, end=' ') | |
print() | |
print('Correct letters:', end=' ') | |
for letter in correctLetters: | |
print(letter, end=' ') | |
print() | |
blanks = '_' * len(secretWord) | |
for i in range(len(secretWord)): | |
if secretWord[i] in correctLetters: | |
blanks = blanks[:i] + secretWord[i] + blanks[i + 1:] | |
for letter in blanks: | |
print(letter, end=' ') | |
print() | |
def getGuess(alreadyGuessed): | |
while True: | |
print('Guess a letter:') | |
guess = input() | |
guess = guess.lower() | |
if len(guess) != 1: | |
print('Please enter a single letter.') | |
elif guess in alreadyGuessed: | |
print('You have already guessed that letter. Choose again.') | |
elif guess not in 'abcdefghijklmnopqrstuvwxyz': | |
print('Please enter a LETTER.') | |
else: | |
return guess | |
def wordMeaning(): | |
s=data.get(secretWord) | |
print(secretWord+" means -",end=" ") | |
print(s[0]) | |
print("\n") | |
print("Do you want to play again?(yes or no)") | |
return input().startswith('y') | |
print('W E L C O M E T O H A N G M A N') | |
f = open("data.json", ) | |
data = json.load(f) | |
missedLetters = '' | |
correctLetters = '' | |
secretWord = getRandomWord() | |
gameIsDone = False | |
while True: | |
displayBoard(missedLetters, correctLetters, secretWord) | |
guess = getGuess(missedLetters + correctLetters) | |
if guess in secretWord: | |
correctLetters = correctLetters + guess | |
foundAllLetters = True | |
for i in range(len(secretWord)): | |
if secretWord[i] not in correctLetters: | |
foundAllLetters = False | |
break | |
if foundAllLetters: | |
print('Yes! The secret word is "' + secretWord + | |
'"! You have won!') | |
gameIsDone = True | |
else: | |
missedLetters = missedLetters + guess | |
if len(missedLetters) == len(HANGMAN_PICS) - 1: | |
displayBoard(missedLetters, correctLetters, secretWord) | |
print('You have run out of guesses!\nAfter ' +str(len(missedLetters)) + ' missed guesses and ' +str(len(correctLetters)) + ' correct guesses,the word was "' + secretWord + '"') | |
gameIsDone = True | |
if gameIsDone: | |
print("You can get the meaning of the word "+secretWord+" by pressing k \n") | |
print("Do you want to play again?(yes or no)") | |
p=input() | |
if p=='k': | |
l=wordMeaning() | |
if l==False: | |
break | |
else: | |
missedLetters = '' | |
correctLetters = '' | |
gameIsDone = False | |
secretWord = getRandomWord() | |
elif p.startswith('y') : | |
missedLetters = '' | |
correctLetters = '' | |
gameIsDone = False | |
secretWord = getRandomWord() | |
else: | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment