Hangman Revealing双打不起作用?

时间:2016-11-24 15:28:13

标签: python python-3.x

所以我有这个刽子手代码...再次,在此先感谢,它一个接一个地显示单词并且工作正常,但是当有多个单词时,例如两个l' s中的hello和两个p&# 39;在苹果中它只显示一个而不是两个。我不知道为什么会这样,而且我已经做了一些谷歌润滑,所得到的是枚举,但我已经尝试过无济于事。 这是代码:

def hangman():
    global fullWordList
    fullWordList = []
    global wrongnums
    wrongnums =[]
    print("What is the word to be guessed?")
    print("Words means that there are no numbers")
    global guessnum
    guessnum=input('>')
    fullWord = guessnum
    for line in fullWord:
      for c in line:
        fullWordList.append(c)
        print (fullWordList)
    print("how many chances?")
    global chances
    chances=int(input('>'))
    print("game in beginning")
#    sleep(5)
#    print ("\n" * 100)
    global blanks
    blanks = '_ ' * len(guessnum)
    print()
    print(blanks)
    guessing()

def guessing():
    global fullWordList
    global chances
    print("guess a letter")
    global guess
    guess=input('>')
    guessloop()

def guessloop():
    global chances
    if guess in fullWordList:
        if guess in fullWordList:
            letterIndex = guessnum.index(guess)
            global blanks
            blanks = blanks[:letterIndex*2] + guess + blanks[letterIndex*2+1:]
            fullWordList.remove(guess);
            global lenWord
            lenWord = len(fullWordList)
            print (fullWordList)
            print (lenWord)
            print ("Guess is correct!")
            if guess in fullWordList:
                guessloop()

            else:
                wrongnums.append(guess)
                print (wrongnums)
                if lenWord == 0:
                    print() 
                    print("Word: ",blanks)
                    win()

                else:
                    print() 
                    print("Word: ",blanks)
                    guessing()

        else:

            if lenWord == 0:
                win()

            else:
                print() 
                print("Word: ",blanks)
                guessing()

    elif guess in wrongnums:
        print("You've guessed that!")
        guessing()

    else:
        chances -=1
        wrongnums.append(guess)
        print (wrongnums)
        print ("Guess is wrong! ", chances, " more failed attempts allowed.")
        if chances == 0:
          gameOver()

        else:
          guessing()

def check():    
    print() 
    print("Word: ",newBlanks)
    guessing2()

def win():
  print("You guessed it well done")
  print("Would you like to play again?")
  PG = input(">")
  if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
    print ("Game will restart in 5 seconds")
    sleep(5)
    hangman()

  else:
    print("Going to main menu in 5 seconds")
    sleep(5)
    start()

def gameOver():
  print("You ran out of guesses, sorry, you lose")
  print("Would you like to play again?")
  PG = input(">")
  if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
    print ("Game will restart in 5 seconds")
    sleep(5)
    hangman()

  else:
    print("Going to main menu in 5 seconds")
    sleep(5)
    start()

现在我也有了一个代码:

def hangman():
    global fullWordList
    fullWordList = []
    global wrongnums
    wrongnums =[]
    print("What is the word to be guessed?")
    print("Words means that there are no numbers")
    global guessnum
    guessnum=input('>')
    fullWord = guessnum
    for line in fullWord:
      for c in line:
        fullWordList.append(c)
        print (fullWordList)
    print("how many chances?")
    global chances
    chances=int(input('>'))
    print("game in beginning")
#    sleep(5)
#    print ("\n" * 100)
    global blanks
    blanks = '_ ' * len(guessnum)
    print()
    print(blanks)
    guessing()

def guessing():
    global fullWordList
    global chances
    print("guess a letter")
    global guess
    guess=input('>')
    guessloop()

def guessloop():
    global chances
    if guess in fullWordList:
        if guess in fullWordList:
            letterIndex = guessnum.index(guess)
            global blanks
            guesses = (guessnum)
            guesses = blanks[letterIndex]
            blanks[letterIndex] = guess
            fullWordList.remove(guess);
            global lenWord
            lenWord = len(fullWordList)
            print (fullWordList)
            print (lenWord)
            print ("Guess is correct!")
            if guess in fullWordList:
                guessloop()

            else:
                wrongnums.append(guess)
                print (wrongnums)
                if lenWord == 0:
                    print() 
                    print("Word: ",blanks)
                    win()

                else:
                    print() 
                    print("Word: ",blanks)
                    guessing()

        else:

            if lenWord == 0:
                win()

            else:
                print() 
                print("Word: ",blanks)
                guessing()

    elif guess in wrongnums:
        print("You've guessed that!")
        guessing()

    else:
        chances -=1
        wrongnums.append(guess)
        print (wrongnums)
        print ("Guess is wrong! ", chances, " more failed attempts allowed.")
        if chances == 0:
          gameOver()

        else:
          guessing()

def check():    
    print() 
    print("Word: ",newBlanks)
    guessing2()

def win():
  print("You guessed it well done")
  print("Would you like to play again?")
  PG = input(">")
  if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
    print ("Game will restart in 5 seconds")
    sleep(5)
    hangman()

  else:
    print("Going to main menu in 5 seconds")
    sleep(5)
    start()

def gameOver():
  print("You ran out of guesses, sorry, you lose")
  print("Would you like to play again?")
  PG = input(">")
  if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
    print ("Game will restart in 5 seconds")
    sleep(5)
    hangman()

  else:
    print("Going to main menu in 5 seconds")
    sleep(5)
    start()


hangman()

这是我编辑它并得到这个错误:

blanks[letterIndex] = guess
TypeError: 'str' object does not support item assignment

我真的很难过,我希望这有足够的数据来帮助。

1 个答案:

答案 0 :(得分:1)

我尝试尽可能少地更改您的代码,但我需要让代码完全运行以解决所有错误,所以我正在分享我对您的代码所做的事情。

你已经确定了两个问题 - 一个是只要有一个匹配,程序就会继续下一个猜测,而不会检查多个正面命中。

第二个问题是你得到的错误是'blanks'是一个字符串,你试图更改特定索引处的字符(第二个代码示例的第43行:blanks[letterIndex] = guess)。字符串是不可变的,因此无法尝试更改字符。围绕这个的方法不止一种 - 我跟(下面)一起将'空白'设置为列表,允许您更改列表中的任何元素(因为列表是可变的)。要向用户显示列表,请尝试''.join(blanks)从列表中生成可显示的字符串。

我对代码进行了一些更改(再次,尝试尊重您的结构和样式)以提出有效的方法:

  1. 删除了变量guessnum(你只需要fullWord)
  2. 将'blanks'从字符串转换为列表 - 现在您可以在猜测正确时进行更改。这与您提出的问题直接相关。
  3. 简化了猜测函数中if ... else语句的流程,经历了三种可能的结果:正确猜测,重复猜测和错误猜测。在那些是一些子检查(如果正确,它是一个胜利?如果不正确,这是一个损失?)我包括一些评论,所以你可以看到做了什么改变和在哪里。
  4. 当猜测正确时,对于发生的每场比赛,“空白”都会相应更新。 (并使用'.join(空格)显示空格以生成元素的字符串。)当猜测正确时,fullWordList也会针对发生的每个匹配进行更新 - 字母将替换为' - '。所以现在检查win是否fullWordList只包含' - '元素。
  5. 仍然没有例外,但是这里有什么用'苹果'这样的词 - 试图赢,输,重复猜测。

    希望它有所帮助 - 让我知道是否有一些不明确的事情。我不想只是重新编写代码,而是希望通过一种方法解决问题。

    还可以进行其他改进,但这至少可以解决你的障碍。

    代码

    def hangman():
        global fullWordList
        fullWordList = []
        global wrongnums
        wrongnums =[]
        print("What is the word to be guessed?")
        print("Words means that there are no numbers")
        # no need to set two variables for same thing (fullWord and guessnum)
        global fullWord
        fullWord=input('>')
        for line in fullWord:
          for c in line:
            fullWordList.append(c)
            print (fullWordList)
        print("how many chances?")
        global chances
        chances=int(input('>'))
        print("game in beginning")
    #    sleep(5)
    #    print ("\n" * 100)
        global blanks
        # treat blanks as a list rather than string (list is mutable)
        blanks = ['- '] * len(fullWord)
        print()
        print(blanks)
        guessing()
    
    def guessing():
        global fullWordList
        global chances
        print("guess a letter")
        global guess
        guess=input('>')
        guessloop()
    
    def guessloop():
        global chances, guess, blanks, blankList, fullWordList
    
    # three possibilities: correct guess, repeated guess, incorrect guess 
    # 1. correct guess
        if guess in fullWordList:
        #generates list of indices of ALL matches  
        # stackoverflow.com/questions/9542738/python-find-in-list
            letterIndex = [i for i, x in enumerate(fullWordList) if x == guess]
            print letterIndex
            for idx in letterIndex:
                # change elements in blank list if there's a match
                blanks[idx] = (guess +' ')
                # update fullWordList to replace correct guesses with '-'
                fullWordList[idx] = ('-')
            # display the updated blank list as a string            
            print ''.join(blanks)
            # replace all occurrences of guess from fullWordList with '-'
            print ("Guess is correct!")
            # add guess to wrongnums to record it as 'already guessed'
            wrongnums.append(guess)
            # check if it's a winning guess
            if fullWordList == ['-']*len(fullWord):
                print() 
                # display updated list as string
                print("Word: ", ''.join(blanks))
                win()
    
    # 2. repeated guess
        elif guess in wrongnums:
            # don't add to wrongnums if already guessed (letter is already in there)
            if guess in wrongnums:
                print("You've guessed that!")
                print ''.join(blanks)
    
    # 3. incorrect guess
        else:
            chances -=1
            wrongnums.append(guess)
            print (wrongnums)
            print ("Guess is wrong! ", chances, " more failed attempts allowed.")
            print ''.join(blanks)
            if chances == 0:
                gameOver()
    
        guessing()
    
    def check():    
        print() 
        print("Word: ",newBlanks)
        guessing2()
    
    def win():
      print("You guessed it well done")
      print("Would you like to play again?")
      PG = input(">")
      if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
        print ("Game will restart in 5 seconds")
        sleep(5)
        hangman()
    
      else:
        print("Going to main menu in 5 seconds")
        sleep(5)
        start()
    
    def gameOver():
      print("You ran out of guesses, sorry, you lose")
      print("Would you like to play again?")
      PG = input(">")
      if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
        print ("Game will restart in 5 seconds")
        sleep(5)
        hangman()
    
      else:
        print("Going to main menu in 5 seconds")
        sleep(5)
        start()
    
    hangman()