Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Does anyone have a shorter version of this hangman game ? # Python 3.7.4 # importing required modules for later import random import time #
Does anyone have a shorter version of this hangman game ? # Python 3.7.4 # importing required modules for later import random import time # main is at the bottom, it calls the two functions choose_word and guessing def choose_word(): # selects a random word from the text file with open('word_list.txt', 'r') as wordlist: # gets the the text file wl = wordlist.readlines() # puts the words in the file into a list return random.choice(wl) # returning the random word as the output of the function def guessing(word): # the actual game # defining lists and variables guesses = 7 # starting number of guesses word_letters = list(word) # puts the letters in the word into a list blanks = '*'*len(word) # makes the * string, same length as word blanks_list = list(blanks) # makes blank string into a list new_blanks_list = list(blanks) # making a working blanks list so it can be checked against the blanks list guess_list = [] # creating empty list for guesses letters = 'abcdefghijklmnopqrstuvwxyz' # defining letters to check for numbers and letters later print (' ',' '.join(blanks_list),' Please guess a letter: ') # printing the blanks and start statement # loop for guess countdown, if it gets to 0 this function ends and goes back to the main function while guesses > 0: guess = str(input('> ')) # user input guess = guess.lower() # dealing with caps # error case loop if len(guess) > 1: # checking for more than one letter entered print ('Enter one letter at a time.') elif not guess in letters: # checking for symbols and numbers print('No symbols or numbers') elif guess == '': # checking for empty entrys print ('Enter a letter.') elif guess in guess_list: # checking for duplication print ('You have already guessed:', ' '.join(str(x) for x in guess_list)) # printing 'You have already guessed:' followed by the list of previous guesses seperated by spaces. else: guess_list.append(guess) # adding the guess to the guess lists # loop to check guess against every letter in the word i = 0 while i < len(word): if guess == word[i]: # if to catch a match new_blanks_list[i] = word_letters[i] # adding a correct guess to the new blanks list in the right place i = i+1 # if for wrong guesses if new_blanks_list == blanks_list: # just checking for no insertion of a letter into the new blanks list print ('You guessed wrong') guesses = guesses - 1 # removing a guess on wrong guess print ('You have', guesses,'guesses left') if guesses > 0: # if you have more guesses it prints the below line before sending you to the start of the while guesses loop print (' ',' '.join(blanks_list)) # printing the current blanks list with spaces between each letter or * print ('Please enter your next guess: ') # if for correct guesses elif word_letters != blanks_list: # catch all blanks_list = new_blanks_list[:] # making blanks_list a copy of new_blanks, adding the new correct letter to blanks_list print (' ',' '.join(blanks_list)) # printing the blanks list with a space between each letter # if for winning if word_letters == blanks_list: # checking is the last correct letter has been entred print (' Congratulations you win') time.sleep (1) # time to read the message #os._exit (0) # ending the script return ('win') # else for correct guess but not win it prints the below line before sending you to the start of the while guesses loop else: print ('Please enter your next guess: ') def main(): # intro para print('', 'Hi JHub ', 'Modual 3 assesment: Hangman but without the actual hangman, which made me sad. ', 'You have 7 lives, to guess a random word from the list in the accompaning word_list.txt file.', 'You are able to guess one letter at a time, with the program either taking a "life" from them if it is an incorrect guess.', 'If correct it will show that the letter is correct and where it appears in the word (i.e. ***a**a*).', 'The game ends when you run out of lives or if you have filled in all the letters of the word.', sep=' ') word = choose_word() # setting word as the output from the choose_word function word = str(word).strip(' ') # removing from the word as in the text file the words are seperated with a return #print(word) # just so I can cheat when testing if guessing(word) == 'win': # if the output of calling the hangman function with the random word is "win" SystemExit # ending the script else: # for the case of losing print (' You lose ') print ('The word was:', word) # giving away the correct answer time.sleep (1) # time to read the message SystemExit # ending the script
Does anyone have a shorter version of this hangman game ?
# Python 3.7.4
# importing required modules for later
import random
import time
# main is at the bottom, it calls the two functions choose_word and guessing
def choose_word(): # selects a random word from the text file
with open('word_list.txt', 'r') as wordlist: # gets the the text file
wl = wordlist.readlines() # puts the words in the file into a list
return random.choice(wl) # returning the random word as the output of the function
def guessing(word): # the actual game
# defining lists and variables
guesses = 7 # starting number of guesses
word_letters = list(word) # puts the letters in the word into a list
blanks = '*'*len(word) # makes the * string, same length as word
blanks_list = list(blanks) # makes blank string into a list
new_blanks_list = list(blanks) # making a working blanks list so it can be checked against the blanks list
guess_list = [] # creating empty list for guesses
letters = 'abcdefghijklmnopqrstuvwxyz' # defining letters to check for numbers and letters later
print (' ',' '.join(blanks_list),' Please guess a letter: ') # printing the blanks and start statement
# loop for guess countdown, if it gets to 0 this function ends and goes back to the main function
while guesses > 0:
guess = str(input('> ')) # user input
guess = guess.lower() # dealing with caps
# error case loop
if len(guess) > 1: # checking for more than one letter entered
print ('Enter one letter at a time.')
elif not guess in letters: # checking for symbols and numbers
print('No symbols or numbers')
elif guess == '': # checking for empty entrys
print ('Enter a letter.')
elif guess in guess_list: # checking for duplication
print ('You have already guessed:', ' '.join(str(x) for x in guess_list)) # printing 'You have already guessed:' followed by the list of previous guesses seperated by spaces.
else:
guess_list.append(guess) # adding the guess to the guess lists
# loop to check guess against every letter in the word
i = 0
while i < len(word):
if guess == word[i]: # if to catch a match
new_blanks_list[i] = word_letters[i] # adding a correct guess to the new blanks list in the right place
i = i+1
# if for wrong guesses
if new_blanks_list == blanks_list: # just checking for no insertion of a letter into the new blanks list
print ('You guessed wrong')
guesses = guesses - 1 # removing a guess on wrong guess
print ('You have', guesses,'guesses left')
if guesses > 0: # if you have more guesses it prints the below line before sending you to the start of the while guesses loop
print (' ',' '.join(blanks_list)) # printing the current blanks list with spaces between each letter or *
print ('Please enter your next guess: ')
# if for correct guesses
elif word_letters != blanks_list: # catch all
blanks_list = new_blanks_list[:] # making blanks_list a copy of new_blanks, adding the new correct letter to blanks_list
print (' ',' '.join(blanks_list)) # printing the blanks list with a space between each letter
# if for winning
if word_letters == blanks_list: # checking is the last correct letter has been entred
print (' Congratulations you win')
time.sleep (1) # time to read the message
#os._exit (0) # ending the script
return ('win')
# else for correct guess but not win it prints the below line before sending you to the start of the while guesses loop
else: print ('Please enter your next guess: ')
def main():
# intro para
print('',
'Hi JHub ',
'Modual 3 assesment: Hangman but without the actual hangman, which made me sad. ',
'You have 7 lives, to guess a random word from the list in the accompaning word_list.txt file.',
'You are able to guess one letter at a time, with the program either taking a "life" from them if it is an incorrect guess.',
'If correct it will show that the letter is correct and where it appears in the word (i.e. ***a**a*).',
'The game ends when you run out of lives or if you have filled in all the letters of the word.',
sep=' ')
word = choose_word() # setting word as the output from the choose_word function
word = str(word).strip(' ') # removing from the word as in the text file the words are seperated with a return
#print(word) # just so I can cheat when testing
if guessing(word) == 'win': # if the output of calling the hangman function with the random word is "win"
SystemExit # ending the script
else: # for the case of losing
print (' You lose ')
print ('The word was:', word) # giving away the correct answer
time.sleep (1) # time to read the message
SystemExit # ending the script
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started