Download presentation
Presentation is loading. Please wait.
Published byEmil Tate Modified over 9 years ago
1
5. Loops 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th Make a program do things over-and-over-and over again. Counting loops; Conditional loops. Games: guessing, ghost, hangman.
2
1. Counting Loop 2 Loop1.py
3
Using the Counting Loop Variable 3 Loop2.py The [ … ] is called a list. More later
4
Using the Counting Loop Var. 4 Loop3.py
5
Counting without Numbers 5 A list can contain anything, not just numbers Coolest.py
6
Counting Loop Using a Range 6 Loop4.py Less typing, but it goes from 1 to 4
7
Eight Times Table 7 This range goes from 1 to 10 EightTimes.py
8
Range with a Step Amount 8 This range goes from 10 to 1 in a step of -1 BlastOff.py This program took 10 secs to run.
9
from turtle import Turtle t = Turtle() t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90) t.forward(100) t.right(90) 2. Drawing a Turtle Square 9 This is Square.py from the Modules slides. The forward() and right() code is repeated four times. A loop will make this code smaller, and easier to change.
10
Square.py with a Loop 10
11
Square1.py with Variables 11 The variables make it easy to change the code. What if numSides == 3 and angle = 120?
12
Shape.py 12
13
For a square, numSides == 4, angle == 90 For a triangle, numSides == 3, angle == 120 The math connection: numSides * angle == 360 Test it. A 5-sided shape (pentagon) means: 5 * angle == 360 so angle = 360/5 Connect No. Sides and Angle 13
14
Revised Shape.py 14
15
In ShapeInput.py read in the numSides data using the turtle command numinput() calculate the angle using the numSides data Make Shape Code Easier to Use 15
16
ShapeInput.py 16
17
17
18
A Square Spiral 18 SquareSpiral1.py
19
Square (90 91 ) + Color 19 SquareSpiral3.py
20
Square Circle 20
21
Square Spiral Again 21 ColorSquareSpiral.py with different colors
22
Spiral with Adjustable Sides 22 ColorSpiral.py
23
Execution: 6 2 sides 23
24
Spiral Steps 24 Spiral Steps.py
25
Execution 25
26
A conditional loop uses a test to decide when to stop looping the same kind of test as in "if" 3. Conditional Loops 26
27
Eight Times Table Again 27 i < 11 is the test the while body is run over and over most while body's need to include an increment WhileLess.py
28
Eight Times Table Problem 28 WhileProb.py i never changes because there is no increment. This means i never reaches 11 and so the loop never ends. I typed ctrl-c
29
While that Likes "3" 29 While3.py
30
Using continue in a Loop 30 Continuer.py continue makes Python start the next loop straight away
31
Using break in a Loop 31 Breaker.py break makes Python leave the loop
32
4. A Number Guessing Game 32 GuessingGame.py
34
34
35
35 Uses two modules; a while loop; if tests
36
5. Ghost Game 36 1 2 3
37
ghostGame.py 37
38
6. Hangman 38 hangman.py
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.