Doubt in this probelm
How does the walk[i-1] functions here??
Hey Niranjan,
Thank you for reaching out!
Consider the following line of code:
![]()
walk[i-1] is the previous position in the random walk. By adding the new randomly chosen step to it, we get the next position in the random walk.
In line 4 of the code, we add a new step_choice to walk[0] - the first position in the list. Then, in line 5, we create a for-loop and the iteration starts at i = 1. In line 6, a new step_choice_2 is created. In line 7, this step_choice_2 is added to the previous position in the random walk, that is walk[i-1] = walk[1-1] = walk[0]. The result is then added to the walk list, in position 1.
During the next iteration, when i=2, we use the value of walk[i-1]=walk[2-1]=walk[1] to calculate walk[2].
Hope this helps!
Kind regards,
365 Hristina
