Lets Learn Python and Pygame Aj Andrew Davison

  • Slides: 31
Download presentation
Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus

Let's Learn Python and Pygame Aj. Andrew Davison, Co. E, PSU Hat Yai Campus E-mail: ad@fivedots. coe. psu. ac. th 5. Loops Make a program do things over-and-over-and over again. Counting loops; Conditional loops. Games: guessing, ghost, hangman. 1

1. Counting Loop 1. py 2

1. Counting Loop 1. py 2

Using the Counting Loop Variable Loop 2. py The [ … ] is called

Using the Counting Loop Variable Loop 2. py The [ … ] is called a list. More later 3

Using the Counting Loop Var. Loop 3. py 4

Using the Counting Loop Var. Loop 3. py 4

Coolest. py Counting without Numbers A list can contain anything, not just numbers 5

Coolest. py Counting without Numbers A list can contain anything, not just numbers 5

Loop 4. py Counting Loop Using a Range Less typing, but it goes from

Loop 4. py Counting Loop Using a Range Less typing, but it goes from 1 to 4 6

Eight Times Table Eight. Times. py This range goes from 1 to 10 7

Eight Times Table Eight. Times. py This range goes from 1 to 10 7

Blast. Off. py Range with a Step Amount This range goes from 10 to

Blast. Off. py Range with a Step Amount This range goes from 10 to 1 in a step of -1 This program took 10 secs to run. 8

2. Drawing a Turtle Square from turtle import Turtle t = Turtle() t. forward(100)

2. Drawing a Turtle Square from turtle import Turtle t = Turtle() t. forward(100) t. right(90) 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. 9

Square. py with a Loop 10

Square. py with a Loop 10

Square 1. py with Variables The variables make it easy to change the code.

Square 1. py with Variables The variables make it easy to change the code. What if num. Sides == 3 and angle = 120? 11

Shape. py 12

Shape. py 12

Connect No. Sides and Angle §For a square, num. Sides == 4, angle ==

Connect No. Sides and Angle §For a square, num. Sides == 4, angle == 90 §For a triangle, num. Sides == 3, angle == 120 §The math connection: § num. Sides * angle == 360 §Test it. A 5 -sided shape (pentagon) means: § 5 * angle == 360 § so angle = 360/5 13

Revised Shape. py 14

Revised Shape. py 14

Make Shape Code Easier to Use §In Shape. Input. py § read in the

Make Shape Code Easier to Use §In Shape. Input. py § read in the num. Sides data using the turtle command numinput() § calculate the angle using the num. Sides data 15

Shape. Input. py 16

Shape. Input. py 16

17

17

A Square Spiral Square. Spiral 1. py 18

A Square Spiral Square. Spiral 1. py 18

Square. Spiral 3. py Square (90 91 ) + Color no need to write

Square. Spiral 3. py Square (90 91 ) + Color no need to write 1. . 100 19

Square Circle 20

Square Circle 20

Color. Square. Spiral. py Square Spiral Again with different colors 21

Color. Square. Spiral. py Square Spiral Again with different colors 21

3. Conditional Loops §A conditional loop uses a test to decide when to stop

3. Conditional Loops §A conditional loop uses a test to decide when to stop looping § the same kind of test as in "if" 22

Eight Times Table Again While. Less. py i < 11 is the test the

Eight Times Table Again While. Less. py i < 11 is the test the while body is run over and over most while body's need to include an increment 23

Flowchart for Conditional Loop program execution i=1 i < 11? no; so finish yes;

Flowchart for Conditional Loop program execution i=1 i < 11? no; so finish yes; keep going loop back. action i=i+1 execution continues. . . 24

While. Prob. py Eight Times Table Problem I typed ctrl-c i never changes because

While. Prob. py Eight Times Table Problem I typed ctrl-c i never changes because there is no increment. This means i never reaches 11 and so the loop never ends. 25

While that Likes "3" While 3. py 26

While that Likes "3" While 3. py 26

Breaker. py Using break in a Loop break makes Python leave the loop 27

Breaker. py Using break in a Loop break makes Python leave the loop 27

Guessing. Game. py 4. A Number Guessing Game 28

Guessing. Game. py 4. A Number Guessing Game 28

30

30

Uses two modules; a while loop; if tests 31

Uses two modules; a while loop; if tests 31