Coding Horror, be scared



# Import random library from random import * def main(): # This is the list of words that could be picked to be guessed words = [ 'these', 'hangman', 'words', 'choose', 'random', 'game', 'chair', 'eleven', 'aluminum', 'pleasant', 'violin', 'tree', 'blanket', 'elephant', 'humidity', 'bottle', 'keyboard', 'pizza', 'turtle', 'ninja', 'storm', 'television', 'sparkle', 'ballpoint', 'stream', 'recorder', 'trombone', 'trumpet', 'silver', 'bronze', 'variable', 'constant', 'confuse', 'list', 'append', 'star', 'rescue', 'function', 'return', 'shopping', 'package', 'ribbon', 'wrapping', 'pleasant', 'harmony', 'cushion', 'choose', 'potable', 'escape', 'conclusion' 'burger', 'king', 'christmas','xbox'] print('Welcome to Hangman!') # This will be the word chosen to be guessed word = random.choice(words) # This variable will counts how many letters to guess are left. lettersleft = len(word) # The player has 6 trys to guess before losing misses = 6 # This list has the bank of words the user guessed guessedLetters = [] # This is the main loop it will run until the player # has either guessed the word or missed 6 times while misses != 0 and '_' in guessedLetters: # This will print the word the player is trying to guess print('Here is the secret word: ', end='') for letter in word: if guessedLetters.count(letter) > 0: print(letter, end = '') else: print('_', end = '') # This will print the already used letters print('Your used letters: ', end='') for letter in guessedLetters: print(letter, end = '') # Print how many misses are left print('Errors left:', misses) # Get the player's guess, and convert it to lower case guess = input('Enter a letter: ').lower() # First, check to see if the player has already guessed # the letter that has been entered if guess in guessedLetters: print('You already guessed that letter!') # Make sure the guess is a letter elif len(guess) != 1 or guess < 'a' or guess > 'z': print('That is not a letter!') # This will run if the input is a letter, and has not been guessed else: # This will append the letter the user guessed to guessedLetters. guess.append(guessedLetters) # This will see how may letters the user guessed. if guess.count(word) > 0: lettersleft - guess.count(word) print(guess, 'was in the secret word') # This will subtract 1 from misses if they did not guess correctly else guess.count(word) = 0: misses - 1 print('That was not in the secret word') # At this point, the game is over. Either the player has # run out of misses (and lost), or guessed the word (and won). # Print an appropriate end-of-game message. # TODO: Add a correct condition to decide whether the game was # won or lost. (Depending on your condition, you may need to # switch the code in the if and else blocks.) if misses = 6: print('Uh oh... game over!') print('The word was', word) else: print('You guessed it!') print(word, 'was the secret word!') main()

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.