简体   繁体   English

如何修复多次输入相同的字符作为猜测?(初学者 Python 项目 - 刽子手游戏)

[英]How to fix inputting the same character multiple times as a guess?(Beginner Python project - Hangman game)

Inputting the same character multiple times doesn't work.多次输入相同的字符不起作用。 Here is my code for the relevant issue Please be noted that I've unnecessarily complicated my code by working on it for a long time to resolve the problem.这是我针对相关问题的代码 请注意,我为解决问题而长时间处理代码不必要地复杂化了我的代码。 I'd really appreciate a correction from anybody.我真的很感谢任何人的更正。 Thanks in advance提前致谢

def gamefunction():
    global chosenword
    global wordlen
    global attempts

    chosenwordlist = []

    tally = 1
    print("\nThe word to guess is....\n")
    print(chosenword)

    display = "_" * len(chosenword)
    print(display)

    for i in chosenword:
        chosenwordlist.append(i)

    print(chosenwordlist)

    while tally <= int(attempts):
        userchoice = input()
        guessedletters = []
        sameletterguess = []

        if len(userchoice.strip()) == 1:
            occcurence1 = chosenword.find(userchoice)

            if (userchoice in chosenword) and (chosenwordlist.count(userchoice) == 1):
                display = display[:occcurence1] + userchoice + display[occcurence1 + 1:]
                guessedletters.append(userchoice)

            elif (userchoice in chosenword) and (chosenwordlist.count(userchoice) > 1):
                sameletterguess.append(userchoice)
                display = display[:occcurence1] + userchoice + display[occcurence1 + 1:]
                guessedletters.append(userchoice)

                print(sameletterguess)
                print(sameletterguess.count(userchoice))

                if sameletterguess.count(userchoice) == 2:
                    occurence2 = chosenword.find(userchoice,chosenword.find(userchoice)+1)
                    display = display[:occurence2] + userchoice + display[occurence2 + 1:]
                    guessedletters.append(userchoice)
                    print(sameletterguess)

                elif sameletterguess.count(userchoice) == 3:
                    occurence3 = chosenword.find(userchoice, chosenword.find(userchoice,chosenword.find(userchoice)+1)+1)
                    display = display[:occurence3] + userchoice + display[occurence3 + 1:]
                    guessedletters.append(userchoice)
                    print(sameletterguess)

                elif sameletterguess.count(userchoice) == 4:
                    occurence4 = chosenword.find(userchoice, chosenword.find(userchoice, chosenword.find(userchoice,chosenword.find(userchoice)+1)+1)+1)
                    display = display[:occurence4] + userchoice + display[occurence4 + 1:]
                    guessedletters.append(userchoice)
                    print(sameletterguess)

            else:
                print("Wrong guess")
                tally = tally + 1

            print("\n" + display)

            if display == chosenword:
                tally = int(attempts) + 1
                print("Congratulations\nYou guessed the word correctly")

        else:
            print("Invalid input\nEnter only a single letter\n")
            print(display)



For further understanding - It seems like the code doesn't run through the if-else loop nested within the 'if len(userchoice.strip()) == 1:' loop为了进一步理解 - 代码似乎没有通过嵌套在 'if len(userchoice.strip()) == 1:' 循环中的 if-else 循环运行

One possible solution for your problem is as below.您的问题的一种可能解决方案如下。 Extensive comments inside the code.代码中的大量注释。

#Defining globals. Note: Using globals is bad practice, use function arguments instead
chosenword = 'boot'
attempts = 4

def gamefunction():
    global chosenword
    global attempts
    
    #Create needed extra variables directly from input
    wordlen = len(chosenword)
    chosenwordlist = list(chosenword)

    tally = 1
    print("\nThe word to guess is....\n")
    print(chosenword)
    
    #Added whitespace for better wordlength visualisation
    display = "_ " * wordlen
    print(display)

    print(chosenwordlist)

    #This holds the letters that have been guessed already
    guessedletters = []

    while tally <= int(attempts):
        display = ''
        userchoice = input()

        if len(userchoice.strip()) == 1:
            #check if the guess is part of the word to guess
            if userchoice in chosenwordlist:
                # Check if letter has been guessed before
                if userchoice in guessedletters:
                    print('You tried that letter already')
                    continue
                else:
                    # Add the guessed letter to the list of all guessed letters and create display
                    guessedletters.append(userchoice)
                    for i in range(wordlen):
                        if chosenwordlist[i] in guessedletters:
                            display+=chosenwordlist[i]
                        else:
                            display+='_ '
            else:
                print("Wrong guess")
                tally = tally + 1

            print("\n" + display)

            if display == chosenword:
                tally = int(attempts) + 1
                print("Congratulations\nYou guessed the word correctly")

        else:
            print("Invalid input\nEnter only a single letter\n")
            print(display)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM