-1

I am making a Hangman Code for a Python class and I can't get a word that has two letters(balloon) to print both letters. This is the specific code snippet. The rest is on the link below. The code is not put together yet.

if letter_ask in word_string:
  location = word_string.index(letter_ask)
  word_blank_list[location] = letter_ask
  print(word_blank_list)
['-', '-', '-', '-', 'o', '-', '-']

Link to my full code

  • 4
    Think about what [`index()`](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) gives you. How can you get the index of the _second_ `L` in `BALLOON`? [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Oct 22 '20 at 19:14

1 Answers1

1

As mentioned in comments, the index is the issue. index returns only first occurance of the element. So you need to search for all occurances of element

So the solution could look like this:

letter_ask = "o"
word_string = "balloon"
word_blank_list = ["-"]*len(word_string)

if letter_ask in word_string:
    locations = [i for i, letter in enumerate(word_string) if letter == letter_ask]
    for location in locations:
        word_blank_list[location] = letter_ask

    print(word_blank_list)
Minarth
  • 314
  • 2
  • 9