Commit cc839155 by Stephen Pearce Committed by Maxim Belkin

### 02-loop.md: Update loop variable names (#738)

parent f3e8cc86
 ... @@ -285,8 +285,8 @@ so we should always use it when we can. ... @@ -285,8 +285,8 @@ so we should always use it when we can. > > > > ## Solution > > ## Solution > > ~~~ > > ~~~ > > for i in range(1, 4): > > for number in range(1, 4): > > print(i) > > print(number) > > ~~~ > > ~~~ > > {: .language-python} > > {: .language-python} > {: .solution} > {: .solution} ... @@ -341,7 +341,7 @@ so we should always use it when we can. ... @@ -341,7 +341,7 @@ so we should always use it when we can. > > ## Solution > > ## Solution > > ~~~ > > ~~~ > > result = 1 > > result = 1 > > for i in range(0, 3): > > for number in range(0, 3): > > result = result * 5 > > result = result * 5 > > print(result) > > print(result) > > ~~~ > > ~~~ ... @@ -375,12 +375,12 @@ so we should always use it when we can. ... @@ -375,12 +375,12 @@ so we should always use it when we can. > (0, 1, 2,...) and the value from the original sequence: > (0, 1, 2,...) and the value from the original sequence: > > > ~~~ > ~~~ > for i, x in enumerate(xs): > for idx, val in enumerate(a_list): > # Do something using i and x > # Do something using idx and val > ~~~ > ~~~ > {: .language-python} > {: .language-python} > > > The code above loops through `xs`, assigning the index to `i` and the value to `x`. > The code above loops through `a_list`, assigning the index to `idx` and the value to `val`. > > > Suppose you have encoded a polynomial as a list of coefficients in > Suppose you have encoded a polynomial as a list of coefficients in > the following way: the first element is the constant term, the > the following way: the first element is the constant term, the ... @@ -389,24 +389,24 @@ so we should always use it when we can. ... @@ -389,24 +389,24 @@ so we should always use it when we can. > > > ~~~ > ~~~ > x = 5 > x = 5 > cc = [2, 4, 3] > coefs = [2, 4, 3] > ~~~ > ~~~ > {: .language-python} > {: .language-python} > > > ~~~ > ~~~ > y = cc[0] * x**0 + cc[1] * x**1 + cc[2] * x**2 > y = coefs[0] * x**0 + coefs[1] * x**1 + coefs[2] * x**2 > y = 97 > y = 97 > ~~~ > ~~~ > {: .output} > {: .output} > > > Write a loop using `enumerate(cc)` which computes the value `y` of any > Write a loop using `enumerate(coefs)` which computes the value `y` of any > polynomial, given `x` and `cc`. > polynomial, given `x` and `coefs`. > > > > ## Solution > > ## Solution > > ~~~ > > ~~~ > > y = 0 > > y = 0 > > for i, c in enumerate(cc): > > for idx, coef in enumerate(coefs): > > y = y + x**i * c > > y = y + coef * x**idx > > ~~~ > > ~~~ > > {: .language-python} > > {: .language-python} > {: .solution} > {: .solution} ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!