The Complete Raspberry Pi Manual Magazine by Store Book

The Complete Raspberry Pi Manual Magazine by Store Book

Author:Store, Book
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2021-07-07T00:00:00+00:00


Hangman Game Script

| =========’’’, ‘’’ luggage responsible ambassador circumstance  congratulate frequent’.split()

 return bank[random.randint(0,len(bank))]

+---+

| | O |

/|\ |

/\ | |

=========’’’] def main():

 game=Hangman(rand _ word())

 while not game.hangman _ over():

game.print _ game _ status()

user _ input=input(‘
Enteraletter: ‘) game.guess(user _ input)

class Hangman:

 def _ _ init _ _ (self,word): self.word=word

self.missed _ letters=[] self.guessed _ letters=[]  game.print _ game _ status()

 if game.hangman _ won():

print (‘
Congratulations! You have won!!’) else:

print (‘
Sorry, you have lost.’)

print (‘The word was‘+game.word)

 def guess(self,letter):

if letter in self.word and letter not in self. guessed _ letters:

 self.guessed _ letters.append(letter) elif letter not in self.word and letter not in self.missed _ letters:

 self.missed _ letters.append(letter) else:

return False

return True  def hangman _ over(self):

return self.hangman _ won() or (len(self.missed _ letters) == 6)

 def hangman _ won(self):

if ‘ _ ’ not in self.hide _ word():  return True

return False

 def hide _ word(self):

rtn=‘’

for letter in self.word:

  if letter not in self.guessed _ letters:    rtn += ‘ _ ’

else:

   rtn += letter

return rtn

def print _ game _ status(self):

print (board[len(self.missed _ letters)]) print (‘Word:‘+self.hide _ word()) print (‘Letters Missed: ‘,)

for letter in self.missed _ letters:  print (letter,)

print ()

print (‘Letters Guessed: ‘,)

for letter in self.guessed _ letters:  print (letter,)

print ()

def rand _ word():

 bank=‘ability about above absolute accessible accommodation accounting beautiful bookstore

 calculator clever engaged engineer enough handsome refrigerator opposite socks interested

 strawberry backgammon anniversary confused dangerous entertainment exhausted impossible

 overweight temperature vacation scissors

 accommodation appointment decrease development

 earthquake environment brand environment necessary  print (‘
Goodbye!
’)

if _ _ name _ _ == “ _ _ main _ _ ”: main()

QUIT() Since this is the last example in our Python code repository, we thought we’d go out with a bang and feature the hangman gallows being drawn with each incorrect guess of the word. Don’t worry if it looks misaligned in the text here, this is merely due to the differences between using the Python IDLE editor and pasting the code into a word processor (which formats things differently).

There’s plenty you can do to improve, enhance and expand on what we’ve presented here. You can include a routine that returns an error if the user enters a number or character. You can include extra points for someone who guesses the entire word in one go rather than one letter at a time and you could perhaps add Chopin’s Funeral March should you lose the game; or something celebratory if you win.

Consider replacing the bank of words too. They’re found under the bank list, and could easily be swapped out for something more difficult. If you download www.github.com/ dwyl/english-words you can find a text document with over 466,000 words. Perhaps you could swap the words in the bank to instead read the contents of the text file:

def rand _ word():  with open(“/home/pi/Downloads/words.txt”, “rt”) as f: bank=f.readlines()

 return bank[random.randint(0,len(bank))]

www.bdmpublications.com 93



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.