1 year ago

#344781

test-img

Tristen Gordon

Element getting removed from every object in nested list unintentionally

Creating a wordle clone in python, and came up with the bug that whenever I check the player's current attempt to a list containing the correct answer for how many attempt they're allowed (Ex. ["steak", "steak", "steak"] if they are only allowed 3 attempts) to be able to recheck the word completely new with each word they input, but whenever I remove a character from one of the items in the list, it removes the character for every item. Any ideas?

    maxAttempts = 5
    load_words()
    wordChoice = random.choice(list_of_words)
    wordChoiceList = list(wordChoice)
    tries = 0
    attempts = []
    testWords = [wordChoiceList] * maxAttempts

    while tries < maxAttempts:

        # TODO Make it look nicer
        if len(attempts) > 0:
            for i in attempts:
                for j in i:
                    pass
        ###

        currentAttempt = input(Fore.LIGHTBLUE_EX + f"Attempt {tries + 1}: ").lower()

        # Correct answer
        if currentAttempt == wordChoice:
            print(f"Correct! The word is {wordChoice}, and you finished in {tries} attempts!")
            playAgain = input("Want to play again? [Y/N] ")
            if playAgain in ["y", "Y", "Yes", "yes", "YES"]:
                main()
            else:
                exit()

        # Wrong type of input checks
        if len(currentAttempt) != wordLength:
            print(f"Word must be exactly {wordLength} letters long")
            continue
        elif currentAttempt not in list_of_words:
            print(f"Not in list of words")
            continue

        # print(testWords)
        result = [None] * len(wordChoice)
        # print(testWords[tries])
        for i in range(len(currentAttempt)):
            # Check if exact position
            if testWords[tries][i] == currentAttempt[i]:
                testWords[tries].pop(i)
                testWords[tries].insert(i, None)
                # print(testWords)
                result[i] = [currentAttempt[i], 2]

            # Check if character only in word
            elif currentAttempt[i] in testWords[tries]:
                testIndex = testWords[tries].index(currentAttempt[i])
                testWords[tries].pop(testIndex)
                testWords[tries].insert(testIndex, None)
                # print(testWords)
                result[i] = [currentAttempt[i], 1]

            # Character is not in word
            else:
                result[i] = [currentAttempt[i], 0]

        # print(testWords[tries])
        print(result)
        attempts.append(result)
        tries += 1

    # Fail by too many attempts
    print(attempts)
    print(f"Too many attempts! Word was {wordChoice}")
    playAgain = input("Want to play again? [Y/N] ")
    if playAgain in ["y", "Y", "Yes", "yes", "YES"]:
        main()
    else:
        exit()

python

nested-lists

wordle-game

0 Answers

Your Answer

Accepted video resources