Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

python.insert(index, obj), seems simple enough, but it's doing something I don't understand

Sometimes it inserts where I expect it to, sometimes not. Insights appreciated.

#import random library
import random
# list for random choice to pick from
list_of_words = ["itty", "bitty", "teeny", "weenie", "tiny", "tidy" , "whitey"]
# random choice stored in word
word = random.choice(list_of_words)
# transform the random word into a list of letters
word_as_list = list(word)
# print the word as a list
print(word_as_list)
# assemling the new word
new_word_list = []


while True:
    # store guessed letter in letter_guess
    letter_guess = input("Guess a letter to make the word: ")
    # get index value if letter_guess is present
    if letter_guess in word:
#place stores the index of the input guess as found in random word
        place = word.index(letter_guess)
        # guess goes into new_word_list at the index and value
        new_word_list.insert(place, letter_guess)
        print(new_word_list)
        if new_word_list == word_as_list:
            print("".join(new_word_list))
            print("You won!")
            break

Hello John,

I think the problem is, that your new list has no placeholders. You start with new_word_list=[] and when you first enter a correct letter, it would be just a list of length 1. I would recommend instead that you define new_word_list=[len(word) * '_'] and then instead of insert you usenew_word_list[place] = letter_guess`, so that the placeholder gets replaced. What do you think?

Thanks Sabine, I'll try that. where did he thing go to give you a "best answer"

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

john larson

The best answer button only shows up on Posts that were posted as an "Answer", Sabine used the "Add Comment". Just as I am doing.

Sabine Maennel

To get credit for the "Best Answer" and to get this Post out of "Unanswered" section, you will have to repost as an Answer instead of a comment. Then John will need to select your answer as "Best Answer". Use the text box at the bottom, underneath the "Add an Answer" heading.

Thank you Chris and John, I did What you proposed.. Good, to notice that there is a difference between comment and answer. I was not aware of that.

Hi Sabine tried this. I'm not sure if this is how you meant. But I'm getting an error at that line

#new_word_list[place] = letter_guess
#IndexError: list assignment index out of range
if letter_guess in word_as_list: 
            for item in word_as_list: 
                if item == letter_guess:  
                    new_word_list=[len(word) * '_']
                    place = word.index(letter_guess)
                    new_word_list[place] = letter_guess #getting an error on this line
                    print(new_word_list)
Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

hey john, I took your snippet and modified it, look for line with comments.

Do you have word in len(word) set somewhere else or was your for loop suppose to be word not item?

if letter_guess in word_as_list: 
            for item in word_as_list: 
                if item == letter_guess:  
                    new_word_list=[len(word) * '_'] #what is the value of word? did you mean item?
                    place = word.index(letter_guess)
                    new_word_list[place] = letter_guess
                    print(new_word_list)

2 Answers

Hello Josh and Chris,

what i meant was this:

# before the loop:
new_word_list=list(len(word) * '_')

# in your while True loop:
if letter_guess in word_as_list:
    place = word.index(letter_guess)
    new_word_list[place] = letter_guess #getting an error on this line
    print(new_word_list)```

Yes, I can see how that would fix it. Thanks so much.

Hello John,

I think the problem is, that your new list has no placeholders. You start with new_word_list=[] and when you first enter a correct letter, it would be just a list of length 1. I would recommend instead that you define new_word_list=[len(word) * '_'] and then instead of insert you usenew_word_list[place] = letter_guess`, so that the placeholder gets replaced. What do you think?