0

I'm having problems wrapping my head around this for loop code. which was part of a Udemy course online and the teacher's code for the hangman project.

This part I understand as we are casting the number of letters in the chosen word into an underscore.

display = []
for _ in range(length_of_word):
    display += "_"

This is the part I just do not understand. line 2 - inside the for loop, position is a variable we use to find a length? line 3 - letter is a variable, but why the [] brackets? line 4 - again the [] brackets and their meaning.

word_list = [item1, item2, item3]
chosen_word = random.choice(word_list)
guess = input("Enter in a letter. ")
length_of_word = len(chosen)word

for position in range(length_of_word):
    letter = chosen_word[position]
    if letter == guess:
        display[position] = letter
print(display)
Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15
thegreek1
  • 5
  • 4
  • The brackets are use for indexing, i.e. for getting the position of an element in some object that can hold many of those elements, like a list. So, for example, `display[0]` is going to give you the first element of `display`. This is very basic Python, so you may want to review your course before attempting this exercise. – Ignatius Reilly Jul 20 '22 at 17:08
  • 1
    You need to review the section of the tutorial on accessing list elements. – Barmar Jul 20 '22 at 17:11
  • Thanks Barmar for pointing me to the the proper tutorial. – thegreek1 Jul 20 '22 at 17:20

3 Answers3

0

range(length_of_word) is a list of numbers with length set to the number given as an argument to the range. Here length_of_word is inputted. If this variable holds the word "hello" the range is [0,1,2,3,4]. This is in the loop used to index the position of a letter in the chosen word and assign it to the variable letter. for example:

word = "hello"
print(word[0]) # prints "h"
letter = word[0] # letter = "h"

You can read about range here: https://docs.python.org/3/library/stdtypes.html#ranges

StianBot
  • 26
  • 2
  • 1
    The question isn't about range. The question is about what `chosen_word[position]` and `display[position]` are for. – Barmar Jul 20 '22 at 17:10
  • Pointing out that range provides a list of numbers that are then used for indexing `chosen_word` makes in more clear what is going on. – StianBot Jul 20 '22 at 17:20
  • Not if he doesn't understand list indexing. – Barmar Jul 20 '22 at 17:22
  • `range(n)` was a list in Python 2, but it isn't in Python 3: https://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3 – slothrop Jul 20 '22 at 17:45
  • Barmar and Stianbot, I appreciate both your help and it was my lack of understanding indexing that led me here. – thegreek1 Jul 20 '22 at 20:11
0

Note: Your chosen_word variable has the value that it took randomly from "word_list".

Here, chosen_word[position] is accessing a letter of that word in the positionth index number. for example position is 2. Then, its chosen_word[2]. if you take "stack" as a word here chosen_word[2] is "a". This letter assigned in your "letter" variable. Then checking with your given letter input. for the condition true, its assigning the value in "display" list with that checked index position and that letter.

Example-

chosen_word = "stack";
guess = "s";
position = 0 ;
letter = chosen_word[position]; //now at oth index value which is "s"

if condition is true now assigning at the same index position of that letter found in the list variable display

display[position] = letter; //display[0] = "s"

I hope this answer could help you.

0

Python is a high-level scripting language. So, when you create a list or a dictionary, the operation seems to be simple, when in fact, lots of lines of code are being executed on the background, instructing the machine to allocate memory in many different ways, to allocate the data structures you need and assign their reference to a variable.

When you execute the following statement, you're creating a list object.

a = []

Which is the same thing as this:

a = list()

list is a global variable storing the list object constructor. Both lines allocate a list object's instance, and assign the structure's reference to the variable a.

As lists are used to store multiple elements inside it, you can create an empty list, or an already pre-allocated list:

a = [] # creates an empty list

b = [0] * 10 # creates a list with 10 positions, all of them filled with 0.

c = [0, 1, 2, 3, 4, 5] # creates a list with the elements 0, 1, 2, 3, 4, 5, inserted in this order

d = [0, 1] * 3 # creates this list: [0, 1, 0, 1, 0, 1]

In order to append data to a list (which can be of any type, including other lists), we can use the .append() method. However, as shown in your example, we can also use ls += [value], which is a sugar syntax to ls = ls + [value]. However, you must take care on using ls += [value] or ls = ls + [value]. As you might have noted, I wrote [value] with brackets, because as an overload operator, the add operations needs a list object on both ends.

Strings are likely a list of characters, so python can implicitely convert "_" to ["_"[0]], meaning it will work. But that's just python easing your life, you shouldn't rely on this mechanism very often.

# both create the same lists:
a = []
for i in range(6):  # In terms of efficiency, complexity is O(n)
    a.append(i)

b = []
for i in range(6):  # In terms of efficiency, complexity is O(n^2), so its slower
    b += [i]

# source: https://www.geeksforgeeks.org/difference-between-and-append-in-python/

Now, as lists contain multiple elements inside it, you may eventually need to access these items. List objects use integers to access each element. Each element is stored in a single position inside your list. There're never holes on your list. If you try to remove an item from the middle, the list will be sorted to fill any gaps.

In order to access a list's element you can use: item = ls[index], where index must be an integer.

You must be careful when accessing items from a list. Indexes ranges from [0, len(list)-1]. If the index is higher than len(list)-1 or lesser than 0, you will be accessing trash not allocated from memory, which can lead to lot's of errors.

On python, you can also use negative integers to access items from its end. The same way as positive indexes range from [0, len(list)-1], negative indexes range from [-len(list), -1]:

ls = [0, 1, 2, 3, 4, 5]
print(ls[-1]) # This will print 5
print(ls[-6]) # This will print 0

So, this covers the basic of why we use brackets. However, there's way more than this, and go read a tutorial if you need more.

Carl HR
  • 776
  • 5
  • 12