Difference between revisions of "Looping Exercises"
From WLCS
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
'''Objective:''' | '''Objective:''' | ||
* To become well-learned in the way of the while loop | * To become well-learned in the way of the while loop | ||
+ | |||
+ | '''Resources:''' | ||
+ | * [[Media:PythonWhileLoops.pptx]] | ||
+ | * [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration] | ||
'''Directions:''' | '''Directions:''' | ||
− | * For each of the following sequences, '''first prompt the user for a number N | + | * For each of the following sequences, '''first prompt the user for a number N''' |
− | |||
* Use while loops to generate the following sequences | * Use while loops to generate the following sequences | ||
+ | * '''Powers of 2''' | ||
+ | ** Print out the first N terms of the sequence | ||
+ | ** 1, 2, 4, 8, 16, 32, 64, 128, ... | ||
− | * | + | * '''Fractions Sequence''' |
− | ** 1, 2, 4, | + | ** Print out the first N terms of the sequence |
+ | ** 1/1, 1/2, 1/3, 1/4, 1/5, ... | ||
+ | * '''Summation up to N''' | ||
+ | ** Use a loop to add up all the numbers from 1 up through N | ||
+ | ** Example: N = 3 would total 6 | ||
+ | ** Example 2: N = 10 would total 55 | ||
− | * Fibonacci Sequence (HINT: use multiple variables) | + | * '''Fibonacci Sequence (HINT: use multiple variables)''' |
+ | ** Print out the first N terms of the sequence | ||
** 0, 1, 1, 2, 3, 5, 8, 13, 21, ... | ** 0, 1, 1, 2, 3, 5, 8, 13, 21, ... | ||
− | + | * '''Factorial Sequence (HINT: use multiple variables)''' | |
− | * Factorial Sequence (HINT: use multiple variables) | + | ** Print out the first N terms of the sequence |
** 1, 1, 2, 6, 24, 120, ... | ** 1, 1, 2, 6, 24, 120, ... |
Latest revision as of 09:28, 20 December 2016
Objective:
- To become well-learned in the way of the while loop
Resources:
Directions:
- For each of the following sequences, first prompt the user for a number N
- Use while loops to generate the following sequences
- Powers of 2
- Print out the first N terms of the sequence
- 1, 2, 4, 8, 16, 32, 64, 128, ...
- Fractions Sequence
- Print out the first N terms of the sequence
- 1/1, 1/2, 1/3, 1/4, 1/5, ...
- Summation up to N
- Use a loop to add up all the numbers from 1 up through N
- Example: N = 3 would total 6
- Example 2: N = 10 would total 55
- Fibonacci Sequence (HINT: use multiple variables)
- Print out the first N terms of the sequence
- 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
- Factorial Sequence (HINT: use multiple variables)
- Print out the first N terms of the sequence
- 1, 1, 2, 6, 24, 120, ...