Hangman question

Joshua Landau joshua at landau.ws
Mon Aug 5 02:11:21 EDT 2013


On 5 August 2013 06:11, <eschneider92 at comcast.net> wrote:

> I'm  on chapter 9 of this guide to python:
> http://inventwithpython.com/chapter9.html     but I don't quite
> understand why line 79 is what it is (blanks = blanks[:i] + secretWord[i] +
> blanks[i+1:]). I particularly don't get the [i+1:] part. Any additional
> information and help would be greatly appreciated!
>

First you should realise that this isn't actually great code by my
standards. There are quite a few un-idiomatic things done, but hopefully
the learning you get outweighs the things you have to unlearn ;).

That said, here's an explanation.

This loop:

    for i in range(len(secretWord)): # replace blanks with correctly
guessed letters
        if secretWord[i] in correctLetters:
            blanks = blanks[:i] + secretWord[i] + blanks[i+1:]

wants to change blanks from a string like "____" to a string like "a_t_".
It has a companion string, secretWord, of the same length (such as "acts").

the "for i in range(len(secretWord)):" loop¹ goes through all the numbers
from 0 to 3 in this case.

"if secretWord[i] in correctLetters:"³ takes the letter from position 0 to
3, which is each of the letters in the secret word in turn, so "a", "c",
"t" and "s" and checks if that letter is in correctLetters.

If it is, we want to do the transformation like "____" -> "a___" and "a___"
-> "a_t_".

This brings us to line 79.

    blanks = blanks[:i] + secretWord[i] + blanks[i+1:]⁴

Let "blanks" be "a___". When i == 2, secretWord[i] == "t" which we can
assume is in correctLetters.

"blanks[:2]" returns "a_" because you're slicing the string up to the
second stop. I don't know how slices have been taught but I'd been taught
like so:

[0]a[1]_[2]_[3]_[4]

so slicing from the start (by missing the start index in the slice) to 2
would return the "a" and "_".

"secretWord[i]" returns "t" as before.

"blanks[i+1:]" == "blanks[2+1:]" == "blanks[3:]" so return the rest *after
skipping one*, so just "_". This means that the additions resolve to:

    blanks = "a_" + "t" + "_"

which results in "a_t_".

Does this help?



¹ You should really use "for i, letter in enumerate(secretWord²):" but I'll
let it pass for simplicity.
² Really, "secret_word" is better than "secretWord", too, but w/e.
 ³ Better written as "if letter in guessed_letters:" given [1] and a
generalisation of [2].
⁴ This is *really* bad practice. Not only is the string rebuilt every time
the loop happens but it's actually rebuilt in part about *three* times per
loop. In this case it's not a big deal, but don't write things like this.⁵
⁵ You should probably use a mutable list which you "".join() OR replace
elements using an iterator (the second of those you can leave for later but
they're not that hard).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130805/e0f14298/attachment.html>


More information about the Python-list mailing list