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
john larson
16,594 PointsI'll buy you a mocha cappuccino if you can help me with this one. Insert() into list very moody
This is the way the code works sometimes. Enter a letter guess in any order and they are placed at the proper index
['e'] #entered one e iterates and places at proper index
['e', 'e']
['e', 'e', 'e']
['w', 'e', 'e', 'e'] #entered w iterates places at 0
['w', 'e', 'e', 'n', 'e'] #entered n iterates, inserts at 3 index
['w', 'e', 'e', 'n', 'i', 'e'] #entered i inserts at 4 index
all the letters are at their proper index even though when I entered e the other indexes were not there yet. This was my intention for the code.
If I do this...
['b', 'i', 't', 't', 'y']
Guess a letter to make the word: y
['y']
Guess a letter to make the word: t
['y', 't'] #the y doesn't hold its index value,it stays at 0
['y', 't', 't'] #enter one t, two t's go in
Guess a letter to make the word: i
['y', 'i', 't', 't'] #i is placed before t
Guess a letter to make the word: b
['b', 'y', 'i', 't', 't'] #everything goes in at the proper index except y
It seems random (to me cause I don't understand) when the code doesn't run as intended.
- this is the code
#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 = []
tries = 0
while True:
tries += 1
# store guessed letter in letter_guess
letter_guess = input("Guess a letter to make the word: ")
# limits number of tries
if tries > len(word_as_list):
print("Sorry, too many tries")
break
else:
if letter_guess in word_as_list: #checks if guess is present in the random choice
for item in word_as_list: #iterate each item in random choice saved as an array
if item == letter_guess: #if the guess is present in the 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) #just to see what I'm working with
if new_word_list == word_as_list:
print("".join(new_word_list)) #turn array back into string
print("You won!")
break
1 Answer
Steven Parker
243,656 Points
Never modify a list while it is being used for iteration.
The modification can throw off the sequence and produce unanticipated results. A good trick to use in such situations is to iterate over a copy of the list, which can be easily made using a slice without parameters. Like this:
for marbles in bundle[:]:
# you can safely modify the original "bundle" here.
john larson
16,594 Pointsjohn larson
16,594 PointsThat's great Steven, Thank you so much! Now, I see you used : which I have seen popping up, but have not seen an explanation of what it is or is doing. One mocha cappuccino for you.
john larson
16,594 Pointsjohn larson
16,594 PointsTrying to apply your concept, modifying bundle[:] doesn't seem have any effect. Did I apply this concept incorectly?