15/19 How does this part work?


#1

for turn in range(4):
print “Turn”, turn + 1

Im having trouble understanding how this did what it did

How is it making the opportunit to try 4 times

how it changing from 1, to 2, to 3 to 4.


#2

Hello :slight_smile:

range function returns a list of integers. You can find more information about range in the official documentation.

This is what range(4) returns:

range(4)
=> [0, 1, 2, 3]

Operator a in x takes first element of x and assigns it to a, then it executes the attached block of code, next it takes the second element of x and assigns it to a and executes the attached block of code and it goes like this to the last element of x. In your code a is turn and x is [0, 1, 2, 3] (result of range(4)).

That is why this code of block is executed four times (because range(4) returns list with 4 elements).


#3

Think I get it, thanks!


#4

You’re welcome :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.