@@ -193,65 +193,66 @@ Here's another loop that repeatedly updates a variable:
193193
194194~~~
195195 length = 0
196- for vowel in 'aeiou':
196+ names = [ 'Curie', 'Darwin', 'Turing']
197+ for value in names:
197198 length = length + 1
198- print('There are', length, 'vowels ')
199+ print('There are', length, 'names in the list. ')
199200~~~
200201{: .language-python}
201202
202203~~~
203- There are 5 vowels
204+ There are 3 names in the list.
204205~~~
205206{: .output}
206207
207208It's worth tracing the execution of this little program step by step.
208- Since there are five characters in `'aeiou' `,
209- the statement on line 3 will be executed five times.
209+ Since there are three names in `names `,
210+ the statement on line 4 will be executed three times.
210211The first time around,
211212`length` is zero (the value assigned to it on line 1)
212- and `vowel ` is `'a' `.
213+ and `value ` is `Curie `.
213214The statement adds 1 to the old value of `length`,
214215producing 1,
215216and updates `length` to refer to that new value.
216217The next time around,
217- `vowel ` is `'e' ` and `length` is 1,
218+ +`value ` is `Darwin ` and `length` is 1,
218219so `length` is updated to be 2.
219- After three more updates ,
220- `length` is 5 ;
221- since there is nothing left in `'aeiou' ` for Python to process,
220+ After one more update ,
221+ `length` is 3 ;
222+ since there is nothing left in `names ` for Python to process,
222223the loop finishes
223- and the `print` statement on line 4 tells us our final answer.
224+ and the `print` function on line 5 tells us our final answer.
224225
225226Note that a loop variable is a variable that's being used to record progress in a loop.
226227It still exists after the loop is over,
227228and we can re-use variables previously defined as loop variables as well:
228229
229230~~~
230- letter = 'z '
231- for letter in 'abc' :
232- print(letter )
233- print('after the loop, letter is', letter )
231+ name = 'Rosalind '
232+ for name in [ 'Curie', 'Darwin', 'Turing' ] :
233+ print(name )
234+ print('after the loop, name is', name )
234235~~~
235236{: .language-python}
236237
237238~~~
238- a
239- b
240- c
241- after the loop, letter is c
239+ Curie
240+ Darwin
241+ Turing
242+ after the loop, name is Turing
242243~~~
243244{: .output}
244245
245- Note also that finding the length of a string is such a common operation
246+ Note also that finding the length of an object is such a common operation
246247that Python actually has a built-in function to do it called `len`:
247248
248249~~~
249- print(len('aeiou' ))
250+ print(len([ 0, 1, 2, 3 ] ))
250251~~~
251252{: .language-python}
252253
253254~~~
254- 5
255+ 4
255256~~~
256257{: .output}
257258
@@ -351,20 +352,19 @@ so we should always use it when we can.
351352> {: .solution}
352353{: .challenge}
353354
354- > ## Reverse a String
355+ > ## Summing a list
355356>
356- > Knowing that two strings can be concatenated using the `+` operator,
357- > write a loop that takes a string
358- > and produces a new string with the characters in reverse order,
359- > so `'Newton'` becomes `'notweN'`.
357+ > Write a loop that calculates the sum of elements in a list
358+ > by adding each element and printing the final value,
359+ > so `[124, 402, 36]` prints 562
360360>
361361> > ## Solution
362362> > ~~~
363- > > newstring = ''
364- > > oldstring = 'Newton'
365- > > for char in oldstring :
366- > > newstring = char + newstring
367- > > print(newstring )
363+ > > numbers = [124, 402, 36]
364+ > > summed = 0
365+ > > for num in numbers :
366+ > > summed = summed + num
367+ > > print(summed )
368368> > ~~~
369369> > {: .language-python}
370370> {: .solution}
0 commit comments