How to replace blanks with guessed letters in Hangman game

时间:2017-04-10 02:21:25

标签: python

Hey so i'm trying to create a simple hangman game in python. So far I've got a very crude version of the program to work, doesn't do everything I want but it works. The Problem I'm having is trying to replace the blank dashes with whatever letter the user inputs. I Honestly have no Idea of where to even begin to try and fix that so any help will be much appreciated.

word = "samsung"
dash = ["_", "_", "_", "_", "_", "_", "_"]
guessedLetters = []


def functionOne():
    print("The Secret word is: ", dash)
    wrongLettersGuessed = " "
    guessLeft = 5
    while guessLeft <= 5:
            guess = input("What is your guess: ")
            if guess in word:
                print("Correct")
                guessedLetters.append(guess)
                print(guessedLetters)
                if len(guessedLetters) == len(word):
                    print("YOU GOT IT !!!")
                    print("The word was: samsung")
                    break

            else:
                wrongLettersGuessed = guess + wrongLettersGuessed
                guessLeft = guessLeft - 1
                print("Incorrect")
                print("Letters guessed", wrongLettersGuessed)
                print(guessLeft)
                if guessLeft <= 0:
                    x = guessLeft + 1
                    print("Sorry you lost the game, the word was samsung")
                    playAgain = input("Would you like to play again (yes or no):")
                    if playAgain == "no":
                        break


functionOne()

1 个答案:

答案 0 :(得分:0)

Simplest way.

>>> dash = ["_", "_", "_", "_", "_", "_", "_"]
>>> dash[0] = 's'
>>> dash
['s', '_', '_', '_', '_', '_', '_']
>>> dash[2] = 'm'
>>> dash
['s', '_', 'm', '_', '_', '_', '_']
>>>