Python- I'm trying to create a word guessing game where you guess the letters. I can't figure out how to get the hidden word to print out with previously guessed letters correctly. Here is my code:
word= w.randomWord() def guessLetters(): """challenges you to guess the letters of a word, the computer will choos the word at random""" length=(len(word)) mask= '-'*len(word) range(0,length) print(mask) guess= input("type a letter to guess: ") if guess in word: print('Correct! try to guess the next letter') correct= word.find(guess) print(mask.replace('-',guess,correct)) mask=(mask.replace('-',guess,correct)) else: print('WRONG!!! try again') print(mask)
mask.replace('-',guess,correct) will replace the first correct '-'s with guess. Not what you want. Remember that a string is just a list of characters. It can be iterated over just like a list, and the individual letters worked with by index ie: mask[ length ]
Join our real-time social learning platform and learn together with your friends!