2017-04-10 99 views
0

嘿所以我想在python中创建一个简单的hang子手游戏。我相对比较新的编程,所以如果我的一些代码似乎没用,请随身携带。到目前为止,我已经有了一个非常粗糙的程序版本,并没有做到我想要的一切,但是它可以工作。我遇到的问题是试图用用户输入的任何字母替换空白破折号。我真的不知道哪里可以开始尝试修复,所以任何帮助都会得到很大的赞赏。Python Hang子手游戏:如何用猜对的字母替换空格

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() 
+0

dash是一个字符串列表,所以你可以使用string.replace(oldstring,new string)来替换空字符串与新字符串。 –

回答

0

最简单的方法。

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