如果语句不再识别while循环python中的输入

时间:2019-07-03 18:35:08

标签: python

我正在用python编写子手游戏,我有一个名为userguess的变量,用户将键入他们想用来猜测的字母。但是,当while循环开始时,它是第二次重复,if语句不再识别用户猜测,并立即得出结论认为这是不正确的猜测。希望您不要介意我作为示例使用的代码量。

movieletters包含分成单词的隐藏单词 defaultletters代表用户必须填写的空白

我尝试通过创建布尔变量来使用异常处理程序,但这还没有完成我需要做的事情。

userguess = input("Guess a letter: ")
        while True:
            for letter in movieletters:
                for defaultletter in defaultletters:
                    if userguess == letter:
                        print("You guessed correctly")
                        score += 1
                        guessedletters.append(userguess)
                        print(score, "/", totalletters)
                        print(guessedletters)
                        print(movie)
                        if score == totalletters:
                            print("\n")
                            print("*************************************************************************************************")
                            print(movie)
                            print("*************************************************************************************************")
                            print("\n")
                            print("You guessed all the letters correctly and uncovered the word!")
                            playagain = input("Would you like to play again? Type y for yes and n for no: ")
                            if playagain == "y":
                                main()
                            else:
                                quit()
                        userguess = input("Guess a letter: ")
                        continue

                    elif userguess != letter:
                        incorrectlyguessed += 1
                        print("INCORRECT!")
                        print(str(incorrectlyguessed) + "/" + str(tally))
                        if incorrectlyguessed == 1:
                            print("""






                                  __________""")
                        elif incorrectlyguessed == 2:
                            print("""

                                  |
                                  |  
                                  |
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 3:
                            print("""
                                  -----------
                                  |
                                  |  
                                  |
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 4:
                            print("""
                                  -----------
                                  |         |
                                  |         
                                  |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 5:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 6:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |         |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 7:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/        
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 8:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/         
                                  |         |
                                  |__________              """)
                        elif incorrectlyguessed == 9:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/         
                                  |         |
                                  |________/_\              """)

                        if incorrectlyguessed == tally:
                            print("GAME OVER!")
                            print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                            print(movie)
                            print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                            playagain = input("Would you like to play again? Type y for yes and n for no: ")
                            if playagain == "y":
                                main()
                            else:
                                quit()
                        userguess = input("Guess a letter: ")
                        continue

我希望输出显示该字符在while循环的第二阶段被识别为隐藏单词中的字母之一。除第一个阶段外,这还会影响所有while循环阶段。

这是当前输出的示例: Output

如果电影是汽车,那么电影信将为['c','a','r','s'] 默认字母为_ _ _ _

1 个答案:

答案 0 :(得分:0)

问题在于,它总是与movieletters列表中的第一个字母匹配。

如果用户猜测电影信中的内容,则使用它来检查其是否匹配

userguess = input("Guess a letter: ")
        if userguess in movieletters:
            print("You guessed correctly")
            score += 1
            guessedletters.append(userguess)
            print(score, "/", totalletters)
            print(guessedletters)
            print(movie)
            if score == totalletters:
                print("\n")
                print("*************************************************************************************************")
                print(movie)
                print("*************************************************************************************************")
                print("\n")
                print("You guessed all the letters correctly and uncovered the word!")
                playagain = input("Would you like to play again? Type y for yes and n for no: ")
                if playagain == "y":
                    main()
                else:
                    quit()
            userguess = input("Guess a letter: ")
            continue

        else :
            incorrectlyguessed += 1
            print("INCORRECT!")
            print(str(incorrectlyguessed) + "/" + str(tally))
            if incorrectlyguessed == 1:
                print("""






                      __________""")
            elif incorrectlyguessed == 2:
                print("""

                      |
                      |  
                      |
                      |
                      |__________              """)
                        elif incorrectlyguessed == 3:
                            print("""
                                  -----------
                                  |
                                  |  
                                  |
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 4:
                            print("""
                                  -----------
                                  |         |
                                  |         
                                  |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 5:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 6:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |         |         
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 7:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/        
                                  |
                                  |__________              """)
                        elif incorrectlyguessed == 8:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/         
                                  |         |
                                  |__________              """)
                        elif incorrectlyguessed == 9:
                            print("""
                                  -----------
                                  |         |
                                  |         0
                                  |        \|/         
                                  |         |
                                  |________/_\              """)

                        if incorrectlyguessed == tally:
                            print("GAME OVER!")
                            print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                            print(movie)
                            print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                            playagain = input("Would you like to play again? Type y for yes and n for no: ")
                            if playagain == "y":
                                main()
                            else:
                                quit()
                        userguess = input("Guess a letter: ")
                        continue