#This is the code for what we went over in the session! word = "pizza" win = False guessed = [] display = ['_', '_', '_', '_', '_'] print("Start guessing!!!!!!!") print("_ _ _ _ _") while win == False: guess = input("Guess a character! ") for x in range(len(word)): if guess in word[x]: display[x] = guess print(display[x], end=" ") if guess not in word: print("Not in word!!!") guessed.append(guess) print("Here is what you've already guessed: ") print(guessed) else: print("That letter was in the word!!!") if '_' not in display: print("YOU WIN!!!!") win = True #######This is the hangman code with extra features added!!######### #additional feature - random numbers for choosing random word in list # from random import randrange word = "secret" #Additional feature - array of words # wordList = ["dog", "cat", "hamster", "fish", "bird"] # random_index = randrange(len(wordList)) # word = wordList[random_index] #Additional feature - counter to keep track of wrong #guesses for displaying the hangman # wrongGuesses = 0 win = False guessed = [] #additional feature - dynamically change display to be the same #length as the chosen word # display = [] # for i in range(len(word)): # display.append('_') display = ['_', '_','_', '_', '_', '_'] name = input("What is your name? \n") print("Hello, " + name, ", time to play hangman!") print("Start guessing...") print("_ _ _ _ _ _") #additional feature - dynamically change length of blank spaces at the start # for x in range(len(display)): # print(display[x], end=" ") while win == False: #Additional feature - print what the user has already guessed guess = input("\nGuess a character: \n") for x in range(len(word)): if guess in word[x]: display[x] = guess print(display[x], end=" ") if guess not in word: print("\n\nThat letter is NOT in the word! \n") guessed.append(guess) print("Here is what you have already guessed:") for x in range(len(guessed)): print(guessed[x], end=" ") #additional feature - print hangman at every wrong guess - this is inside the for loop # wrongGuesses += 1 # if wrongGuesses == 1: # print("\n-----\n| |\n| O\n|\n|\n|\n_____") # elif wrongGuesses == 2: # print("\n-----\n| |\n| O\n| |\n| |\n|\n_____") # elif wrongGuesses == 3: # print("\n-----\n| |\n| O\n| /|\n| |\n|\n_____") # elif wrongGuesses == 4: # print("\n-----\n| |\n| O\n| /|\\ \n| |\n|\n_____") # elif wrongGuesses == 5: # print("\n-----\n| |\n| O\n| /|\\ \n| |\n| /\n_____") # elif wrongGuesses == 6: # print("\n-----\n| |\n| O\n| /|\\ \n| |\n| / \\ \n_____") # print("\nYOU LOSE!") # break else: print("\n\nThat letter WAS in the word! \n") if '_' not in display: print("YOU WIN") win = True