#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0

    Using [:i] and [i:]?


    hi, I'm studying an example of a hangman program. here's the bit of the code important to my question:

    Code:
    def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
        print(HANGMANPICS[len(missedLetters)]) #print item in HANGMANPICS corresponding with
                                               #length of missedLetters
        print()
    
        print('missed letters:', end=' ')
        for letter in missedLetters: #do statements of loop for each variable in parameter
            print(letter, end=' ') #print every missed letter
        print()
    
        blanks = '_' * len(secretWord)
    
        for i in range(len(secretWord)): #replace blanks with correctly guessed letters
            if secretWord[i] in correctLetters: #if item of secretWord string is in correctLetters
                blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
    
        for letter in blanks: #show the secret word with spaces inbetween each letter
            print(letter, end=' ')
        print()
    the line I'm confused by is this:

    Code:
    blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
    this line is supposed to replace underscores with the correct letters guessed in the game. I've never seen [i] used with colons before this example and the explanation of how it works doesn't make sense to me
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    In most ways a string is a list of characters. So 'python' can be thought of as ['p', 'y', 't', 'h', 'o', 'n']. In Python this isn't always true (where in C or Haskell it is always true (and in C even truer if you admit that everything is an integer that is told to pretend it is something else)), but generally thinking of strings this way is useful.

    Bracket ranges on lists can be of the form [index] or [start:stop] or [start:stop:step], and omitting either of start or stop will assume the very first or very last element in the list.

    The code is making "blanks" be a string exactly the same length as "secretWord", and sandwiching the letter desired from the secret word between two lists of underscores that are known to be the same length.
    Code:
    >>> secret_word = 'foobar'
    >>> blanks = '_' * len(secret_word)
    >>> blanks
    '______'
    >>> secret_word
    'foobar'
    >>> blanks[:2]
    '__'
    >>> secret_word[2]
    'o'
    >>> blanks[2+1:]
    '___'
    >>> blanks[:2] + secret_word[2] + blanks[2+1:]
    '__o___'
    >>> for i in range(len(secret_word)):
    ...   print blanks[:i] + secret_word[i] + blanks[i+1:]
    ... 
    f_____
    _o____
    __o___
    ___b__
    ____a_
    _____r
    The reason for the "+1" on the second index range is that we are skipping the index of the target letter itself.

    There are other ways to manage this task, but this is probably the most readable I've seen.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0
    I'm starting to understand, but I have a few questions:

    so [:i] and [i+1:] would be the start and end (plus 1) respectively?

    for the first and last indexes how does python know to print a letter instead of an underscore?

    and how does putting +1 make it so python knows to skip over the printed letter?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    It's not that [:i] and [i:] are the "start" and "end" so much that they are the segment from index 0 to "i" and segment from index "i" to the end of the array. "i" is just a variable name here that is changing each time the for loop iterates and we are using it as the index value of the two strings blank and secret_word.

    There is no better way to understand how slicing works than to open a Python interpreter and play with it yourself.

    Link:
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    6
    Rep Power
    0
    thanks for the link

    so from what I gathered from the section on slicing, the line in question would be:

    blanks up to correct letter + correct letter + blanks after correct letter
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    Precisely.

    Once you get a little more used to reading Python idioms the for loop I wrote as an example above will read exactly the same way in your mind without needing the intermediate step of translating it to English first.

IMN logo majestic logo threadwatch logo seochat tools logo