Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looping Topic 4.

Similar presentations


Presentation on theme: "Looping Topic 4."— Presentation transcript:

1 Looping Topic 4

2 Looping We often want to repeat an action Example
print an asterisk 10 times counter = 1 while counter <= 10 True print(‘*’) counter += 1 False

3 For Loop Looping in a range is so common, there is a “For” loop
One use of a For loop is executing a certain number of times

4 Looping with Trinket What do you think the following code will do?
Compare the block based to text based coding.   Mention that we do not have to add one each time to increase count. If participants don’t notice, point to the need for colon and to indent when working with text based programming. *Index for block based starts at 1. However, for text based, it starts at 0. 

5 Looping with Trinket (Text Based)
What numbers will be printed?  The following loop iterates over numbers 0-9. If a single value is provided within the range function, then the sequence will start at 0 and will end with the Number minus one(n-1)

6 For Loop The for loop below starts with counter taking on the values 1 up to BUT NOT INCLUDING 11 with a step size of 1 for counter in range(1, 11, 1): print('*')

7 Looping with Trinket (Text Based)
What number(s) will be printed?  The following loop iterates over numbers within 1-9 with a step value of 2. That means, only 1,3,5, 7and 9 will be printed. 

8 Difference between for loops and while loops
While loops use a condition to determine the number of steps -Example, while you are not tired, do pushups  For loops have a set number of steps  -Example, you must do 10 pushups 

9 While Loop Application
In a loop, continually request numbers as input and then print them as long as they are even Example output: What is your number? What is your number? What is your number? 3 You entered an odd number. Giving example output often clears up confusion about the problem statement!

10 While Loop Application – Solution (Blocks)

11 While Loop Application – Solution (Text)
num = int(input('Enter an integer: ')) while num % 2 == 0:     print(num)     num = int(input('Enter an integer: ')) print(‘You entered an odd number’)

12 Common Problems An ”infinite loop” occurs when a while loop goes forever and never meets the condition to stop! If you accidentally initialize a variable to something that does not pass the condition, it will not enter the loop i = 1 while i < 10: print(i) The first example will print 1 forever since i is not changing (being incremented) within the loop. The second example won’t print anything at all. i = 11 while i < 10: print(i) i += 1

13 Your Turn Can you write a program that accepts a number as input then prints a multiplication table for that number from 1 to 10?

14 Your Turn For example, upon entering the number 2, the program would output:

15 Trinket – Shapes with Turtle
We can use the blocks within turtle to create shapes. Spend a few minutes to help participants feel comfortable with the blocks within turtle. Blocks such as, shape, move, turn, pen, and fill.

16 Trinket – Regular Triangle Explore
We will create a regular (congruent sides/angles) triangle. What numbers should we replace the two questions marks with? Why?      We need a 3 for repeat since we have a triangle. Also, we need to turn 120 since the interior angle is 60 degrees ( equilateral triangle), the exterior will be 120 degrees.  For non-math participants, please address what is a polygon and what is a regular polygon (all sides are equal and all angles are equal).  Polygon is a shape that is closed and made with segments. Examples are, pentagon, hexagon and heptagons.

17 Trinket – Regular Polygon Application
Write a program that takes in the number of sides (greater or equal to 2) in a regular polygon and creates the corresponding regular polygon. For example, if you input a 4, your turtle should draw a square. 

18 Trinket – Regular Polygon Application
Write a program that takes in the number of sides (greater or equal to 2) in a regular polygon and creates the corresponding regular polygon. For example, if you input a 4, your turtle should draw a square.  Have participants verbalize what each block is doing. 


Download ppt "Looping Topic 4."

Similar presentations


Ads by Google