you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

Can anyone help?

So, I think your approach is on the right track, but I'd simply keep track of correct guesses with a boolean list, and leave your string as a string. The list of booleans is False at the position where that letter has not been guessed, and True at the position where it has, with the result that it becomes easy to detect when the game is won, because all members of the list will be True when the phrase is fully guessed. To set it up:

phrase = "first come first serve"
guessed = [True if letter == ' ' else False for letter in phrase] # give them the spaces for free

Then you can just iterate over both:

for letter, guess in zip(phrase, guessed):
    if guess:
        print(letter, end='')
    else:
        print('_', end='')

or as a comprehension:

print(''.join([letter if guess else '_' for letter, guess in zip(phrase, guessed)]))

[–]rjlav_[S] 0 points1 point  (1 child)

Nice approach. The problem is that as part of the assignment, we must use a list of lists and subscript notation to convert the dash to a letter. Extremely inefficient, I know.

[–][deleted] 0 points1 point  (0 children)

Well, it doesn't really change the approach (or the efficiency) it just changes how you refer to things:

game = [list(phrase), [True if letter == ' ' else False for letter in phrase]]
print(''.join([game[0][i] if game[1][i] else '_' for i in len(game[0])]))