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

问题描述

多次输入相同的字符不起作用。这是我针对相关问题的代码 请注意,我为解决问题而长时间处理代码不必要地复杂化了我的代码。我真的很感谢任何人的更正。提前致谢

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)



为了进一步理解 - 代码似乎没有通过嵌套在 'if len(userchoice.strip()) == 1:' 循环中的 if-else 循环运行

标签: python

解决方案


您的问题的一种可能解决方案如下。代码中的大量注释。

#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)

推荐阅读