Jump to content

Question about list Slicing (in a Hangman game)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Cory Duchesne

Cory Duchesne

    Newbie

  • Members
  • PipPip
  • 10 posts
Hello all, :)

I'm new to Python, and have been doing online tutorials. Python is also my first programming language, I know no others, except for html and css, which don't really count.

I am trying to get through a tutorial that teaches how to make a hangman game.

Here is the tutorial:

Hangman

And here is the source code to the game:

Source Code of Hangman Game

***

alright, so what is my difficulty exactly? My difficulty is in understand how the colon functions as a slice within list indexes.

I'll give you an an example of a simplified piece of code I took from the hangman tutorial:

secretWord = 'monkey'


length = len(secretWord) # this equals 6


blanks = '_' * len(secretWord) # this produces 6 blanks


for i in range(length): # i = [0, 1, 2, 3, 4, 5]


    x = blanks[:i]

    

    print(x)   # printing X causes a list of only 5 items.   Why?   Why aren't there six items? 


So when you run this code, you'll find that it produces a list of 5 items. The first item is a single blank, the second is two blanks, the third is three blanks, and so on.

But it stops at 5 blanks. Why? The range is clearly from 0 to 5, which means you have six instances. Zero should produce a single blank, one should produce then next blank, then two, three, right up to 5, which would equal 6 blanks in total.

Now, to make matters even more confusing, please observe what happens when I put the colon on the right side of the list index:

secretWord = 'monkey'


length = len(secretWord) # this equals 6


blanks = '_' * len(secretWord) # this produces 6 blanks


for i in range(length): # i = [0, 1, 2, 3, 4, 5]


    x = blanks[i:]

    

    print(x)   # printing X causes a list of only 5 items.   Why?   Why aren't there six items? 


This time the list of blanks conforms to what I would actually expect, you have 6 list items. The first list item is string of 6 blanks, then next line down is a string of 5 blanks, then 4 blanks, and so on, right down to the last single blank. This makes sense.

So how come when the colon is placed on the left side of a list index, it fails to conform to logic one would expect?

#2
Cory Duchesne

Cory Duchesne

    Newbie

  • Members
  • PipPip
  • 10 posts

Quote

So when you run this code, you'll find that it produces a list of 5 items. The first item is a single blank, the second is two blanks, the third is three blanks, and so on.

aha! I just noticed that the code actually produces a list of 6 items, except that the very first item is invisible, it is zero, and then the second line is 1 blank.

I didn't realize that python basically takes the range (0 to 5) and does multiplication.

So this is what i think is happening behind the scenes:

0 x blank = 0 blanks
1 x blank = 1 blank
2 x blank = 2 blank
....
....
5 x blank = 5 blank

***

No why isn't this happening on the second code?
x = blanks[i:]

having the colon on the right of a list index with a range of zero to five, must mean that it starts at 6 and works it's way down. Not perfectly clear why they made it this way, it is confusing, but I'm not nearly as confused as I was previously.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Okay, this is actually a holdover from C, and is standard convention among nearly every programming language I know. It's that array indices, especially when talking about groups, always include the first and exclude the last. What this means is that
print infiniteListOfNums[:10]
will produce [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. This is because the first digit, 0, is counted as a number in the set too. If you say myList[1], you want the second element in the list, not the first one. myList[0] is the first element. As such, it made sense to exclude the last value, it's commonly notated like this:

[0..10)

And now you know. ^_^
Wow I changed my sig!