Skip to content

Commit 7905622

Browse files
author
giumas
committed
added exercise for while loop in 004
1 parent 254904c commit 7905622

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

004_Loops.ipynb

+50-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
"cell_type": "markdown",
4949
"metadata": {},
5050
"source": [
51-
"Similarly to the [`if`](003_Conditional_Execution.ipynb#Applying-Conditions-with-if,-elif,-and-else) statement, a `for` loop requires that:\n",
51+
"Similarly to the [`if`](003_Conditional_Execution.ipynb#Applying-Conditions-with-if,-elif,-and-else) statement, a `for` loop has several formatting requirements:\n",
5252
"\n",
53-
"- There must be a subsequent element in the sequence; the condition following the `for` keyword must be `True`. \n",
54-
"- You add a `:` at the end of the `for` condition.\n",
55-
"- You indent all the lines of code that have to be executed in each iteration."
53+
"- It begins with a `for` condition like `for <variable> in <sequence>` where:\n",
54+
" - `<sequence>` is the list over which to iterate.\n",
55+
" - `<variable>` is the name of a variable that will store the current value on each iteration. \n",
56+
"- A `:` at the end of the `for` condition.\n",
57+
"- The indentation of all the lines of code that have to be executed in each iteration."
5658
]
5759
},
5860
{
@@ -414,6 +416,50 @@
414416
"The <button class='btn btn-default btn-xs'><i class=\"fa fa-stop\"></i></button> button in the Toolbar stops the execution of a **Code** cell. "
415417
]
416418
},
419+
{
420+
"cell_type": "markdown",
421+
"metadata": {},
422+
"source": [
423+
" "
424+
]
425+
},
426+
{
427+
"cell_type": "markdown",
428+
"metadata": {
429+
"solution2": "hidden",
430+
"solution2_first": true
431+
},
432+
"source": [
433+
"<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n",
434+
"\n",
435+
"Add the code required to pop and then print all the values from the `sal_list`:"
436+
]
437+
},
438+
{
439+
"cell_type": "code",
440+
"execution_count": null,
441+
"metadata": {
442+
"solution2": "hidden"
443+
},
444+
"outputs": [],
445+
"source": [
446+
"sal_list = [32.1, 33.7, 35.0, 32.5, 31.9] # water salinity in PSU\n",
447+
"\n",
448+
"while len(sal_list) > 0:\n",
449+
" \n",
450+
" removed_value = sal_list.pop()\n",
451+
" print(\"Removed element: \" + str(removed_value))"
452+
]
453+
},
454+
{
455+
"cell_type": "code",
456+
"execution_count": null,
457+
"metadata": {},
458+
"outputs": [],
459+
"source": [
460+
"sal_list = [32.1, 33.7, 35.0, 32.5, 31.9] # water salinity in PSU"
461+
]
462+
},
417463
{
418464
"cell_type": "markdown",
419465
"metadata": {},

005_Write_Your_Own_Functions.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@
495495
"cell_type": "markdown",
496496
"metadata": {},
497497
"source": [
498-
"## Functions and their local variables"
498+
"## Functions and Their Local Variables"
499499
]
500500
},
501501
{

0 commit comments

Comments
 (0)