I am writing a Hangman program during my free time at school, as an entry level project for Python, and I have finally run into an obstacle.
print("Welcome to Tyler's Hangman game!!!! Player one, input a word for player two to guess, lowercase!\n")
playAgain = True
guessing = True
#assure game starts in the True state
while playAgain == True:
word = str(raw_input())
wordLength = int(len(word))
board = "_ " * wordLength
lives = 9
#initialize/reset variables on a new play
while guessing == True:
print("\n" * 15)
print("Now its player two's turn to make a guess! You have 9 lives! Only one letter at a time, lowercase!\n")
print(board + "Lives: " + str(lives) + "\n")
#print board + lives
guess = str(raw_input())
if guess in word:
print("Correct!")
word.index(guess)
else:
print("Wrong.")
score -= 1
#check guess and change board
if guess in word:
print("Correct!")
word.index(guess)
I'm going to propose an alternative solution to your real problem. Mutating strings is not very idiomatic in Python and is prone to errors. My approach instead will be to keep track of which letters have been guessed and then compute the board state from that and the actual answer.
answer = 'hello world'
guessed = ['a', 'e', 'm', 'r']
def compute_board(answer, guessed):
return ''.join(c if c == ' ' or c in guessed else '_' for c in answer)
compute_board(answer, guessed) # gives _e___ __r__
Now you can easily get the number of guesses from the length of the guessed
list, you can check for repeated guesses easily by checking if their guess is already in the list, and you can add guesses easily by just adding them to the end of the list.