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 Basics (2015) Letter Game App Letter Game Introduction

Mark Kohner
Mark Kohner
1,121 Points

Letter game introduction, how does appending the guess to the correct position work?

if guess in secret_word: good_guesses.append(guess) if len(good_guesses) == len(list(secret_word)): print('You win! The word was {}'.format(secret_word)) break

how does this code work? I do not understand how it can find the correct position for the character guessed. For example if the word is apple and the guess was 'p' how does it check that we are appending the guess to the correct position?

why won't it append like A__LEP and put the P at the end? or if the guess as 'A' and we have _PPLEA ? How does it know to append to the matching position? How would the 'p's append if there are two?

1 Answer

Steven Parker
Steven Parker
231,236 Points

The section of the program you quoted here is not involved with the placement of the letters. This section is just determining if a guess is good (if it's anywhere in the word) and if the game has been won.

Elsewhere in the program, the placement is determined by a loop that iterates over the letters of the word, and if the letter has been guessed it prints it, and if not guessed it prints an underline instead.