David Pearson David Pearson - 6 months ago 5
Python Question

Python Hangman game and replacing multiple occurrences of letters

This is my first year programming with Python.
I am trying to create a hangman game.

So my questions: how do I check for


  1. If the person is guessing a letter that has already been guessed and where to include it.

  2. Check they are inputting a valid letter not multiple letters or a number.

  3. What happens if it's a word such as 'Good' and they guess an 'o' at the moment my code breaks if this is the case. Also where on Earth would this be included in the code?



Here is my code

import random
import math
import time
import sys


def hangman():
if guess == 5:
print " ----------|\n /\n|\n|\n|\n|\n|\n|\n______________"
print
print "Strike one, the tigers are getting lonely!"
time.sleep(.5)
print "You have guessed the letters- ", guessedLetters
print " ".join(blanks)
print

elif guess == 4:
print " ----------|\n / O\n|\n|\n|\n|\n|\n|\n______________"
print
print "Strike two, pressure getting to you?"
time.sleep(.5)
print "You have guessed the letters- ", guessedLetters
print " ".join(blanks)
print

elif guess == 3:
print " ----------|\n / O\n| \_|_/ \n|\n|\n|\n|\n|\n______________"
print
print "Strike three, are you even trying?"
time.sleep(.5)
print "You have guessed the letters- ", guessedLetters
print " ".join(blanks)
print

elif guess == 2:
print " ----------|\n / O\n| \_|_/ \n| |\n|\n|\n|\n|\n______________"
print
print "Strike four, might aswell giveup, your already half way to your doom. Oh wait, you can't give up."
time.sleep(.5)
print "You have guessed the letters- ", guessedLetters
print " ".join(blanks)
print

elif guess == 1:
print " ----------|\n / O\n| \_|_/ \n| |\n| / \\ \n|\n|\n|\n______________"
print
print "One more shot and your done for, though I knew this would happen from the start"
time.sleep(.5)
print "You have guessed the letters- ", guessedLetters
print " ".join(blanks)
print

elif guess == 0:
print " ----------|\n / O\n| \_|_/ \n| |\n| / \\ \n| GAME OVER!\n|\n|\n______________"

print "haha, its funny cause you lost."
print "p.s the word was", choice
print


words = ["BAD","RUGBY","JUXTAPOSED","TOUGH","HYDROPNEUMATICS"]

choice = random.choice(words)


lenWord = len(choice)

guess = 6

guessedLetters = []

blanks = []

loading = ".........."

print "Loading",
for char in loading:
time.sleep(.5)
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.5)
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

print "Great I'm glad to see you have finally woken."
time.sleep(1)
raw_input("Did you have a nice journey? ")
time.sleep(2)

print """
Oh wait, I don't care.
I have brought you to this island simply for my amusment. I also get paid to do this.
"""

time.sleep(2)
print"""
Don't worry this isn't all for nothing.
I'm sure the tigers will enjoy your company.
"""

time.sleep(2)
print"""
Hold on, let us make things interesting.
"""

time.sleep(2)
print "I will let you live if you complete an impossible game!"
time.sleep(2)
print "A game know as hangman!"
time.sleep(2)
print "HAHAHAHAHAH, I am so evil, you will never escape!"
time.sleep(2)
print "Enjoy your stay :)"

time.sleep(1)
print
print "Alright lets begin! If you wish to guess the word type an '!' and you will be prompted"
time.sleep(.5)
print

for s in choice:
missing = choice.replace(choice, "_")
blanks.append("_")
print missing,
print
time.sleep(.5)

while guess > 0:
letterGuess = raw_input("Please enter a letter: ")
letterGuess = letterGuess.upper()

if letterGuess == "!":
print "If you guess the FULL word correcly then you win, if incorrect you die. Simple."
fullWordGuess = raw_input("What is the FULL Word? ")
fullWordGuess = fullWordGuess.upper()
if fullWordGuess == choice:
print "You must have hacked this game"
time.sleep(.5)
print "Looks like you beat an impossible game! \nGood Job \nI'll show myself out."
break
else:
print "You lost, I won, you're dead :) Have a nice day!"
print "P.S The word was ", choice
break
else:
print

if letterGuess in choice:
location = choice.find(letterGuess)

blanks.insert(location, letterGuess)
del blanks[location+1]

print " ".join(blanks)

guessedLetters.append(letterGuess)
print
print "You have guessed the letters- ", guessedLetters

if "_" not in blanks:
print "Looks like you beat an impossible game! \nGood Job \nI'll show myself out."
break
else:
continue

else:

guessedLetters.append(letterGuess)
guess -= 1
hangman()

Answer

I did not really follow your code since there seems to be something wrong with your indentation, and the many print's confused me a little, but here is how I would suggest soving your Questions:

1.1. Create a list of guessed characters:

guessed_chars = []
if(guess in guessed_chars):
    guessed_chars.append(guess)
    #do whatever you want to do with the guess.
else:
    print("You already guessed that.")
    #do what ever you want to do if you already guessed that.

1.2 Lets say the word the player has to ges is word = "snake". To get the indices (since a string is just a list of charcters in python, only that it is immutable) where the guessed letter is in the word you could then use something like:

reveal = "_"*len(word)
temp = ""
for i, j in enumerate(word):
    if j == guess:
        temp += guess
    else:
        temp += reveal[i]

reveal = temp
print(reveal)
  1. Maybe not fancy but it should work:

    if (guess not in [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]):

  2. with my suggestion for 1.2 you would just have to check if(word==reveal): no problem with duplicate charcters.

Since I am not just a hobbyist there would probably be a more professional way though.