+ All Categories
Home > Documents > Noadswood Science, 2014. To understand the flow procedure when writing programs Thursday, January...

Noadswood Science, 2014. To understand the flow procedure when writing programs Thursday, January...

Date post: 02-Apr-2015
Category:
Upload: jayden-whorton
View: 215 times
Download: 2 times
Share this document with a friend
Popular Tags:
21
Noadswood Science, 2014
Transcript
Page 1: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Noadswood Science, 2014

Page 2: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

To understand the flow procedure when writing programs

Tuesday, April 11, 2023

Page 3: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Input > Output

A program takes input (information in), processes it (or changes it) and then gives back the results (output)

Input > Processing > Output

Input (input command, keyboard, mouse) Processing (variables, mathematicise, loops, branches,

functions) Output (print command, screen, graphics)

Take a look at the script for the Ghost game previously written: identify the input, processing and output functions

Page 4: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Ghosts

#Ghost Gamefrom random import randintprint('Ghost Game')feeling_brave = Truescore = 0 while feeling_brave: ghost_door = randint(1, 3) print('Three doors ahead...') print('A ghost behind one.') print('Which door do you open?') door = input ('1, 2, or 3?') door_num = int(door) if door_num ==ghost_door: print('GHOST!') feeling_brave = False else: print('No ghost!') print('You enter the next room.') score = score + 1print('Run away!')print('Game over! You scored' , score)input("\n\nPress the enter key to exit.")

Page 5: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Ghosts

#Ghost Gamefrom random import randintprint('Ghost Game') Output > print is used to display informationfeeling_brave = Truescore = 0 Processing > variables keep track of the scorewhile feeling_brave: ghost_door = randint(1, 3) Processing > variables keep track of the score print('Three doors ahead...') print('A ghost behind one.') print('Which door do you open?') door = input ('1, 2, or 3?') Input > taken from the keyboard door_num = int(door) if door_num ==ghost_door: print('GHOST!') feeling_brave = False else: print('No ghost!') print('You enter the next room.') score = score + 1print('Run away!')print('Game over! You scored' , score)input("\n\nPress the enter key to exit.")

Page 6: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Simple Commands

Here are some basic commands that will be useful when using Python…

Command Python

Run program “Run” menu

Stop program “CTRL-C” in shell

Text to screen print(‘Hello!’)

Set variable to number

magic_number = 123

Set variable to text string

word = ‘Noadswood’

Read text from keyboard into a variable

age = input(‘age?’)print(‘I am ‘ + age)

Command Python

Add a number to a variable

cats = cats + 1

Add a + 2

Subtract a - 2

Multiply a * 2

Divide a / 2

Forever loopwhile True: jump ()

Page 7: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Simple Commands

Here are some basic commands that will be useful when using Python…

Command Python

Loop 10 timesfor i in range (10): jump ()

Is equal to? a == 2

Is less than? a < 2

Is more than? a > 2

NOT not

Command Python

OR or

AND and

If thenif a == 2: print(‘Hello!’)

If then else

if a == 2: print(‘Hello!’)Else print(‘Goodbye!’)

Page 8: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

More Complex Commands Here are some more complex commands

Command Python

Loops with conditions

while roll != 6: jump ()

Waitfrom time import sleepsleep(2)

Random numbersfrom random import randintroll = randint(1, 6)

Define a function or subprogram

def jump(): print(‘Jump!’)

Call a function or subprogram

jump()

Define a function or subprogram with input

def greet(who): print(‘Hello ‘ + who)

Command Python

Call a function or subprogram

greet(‘chicken’)

Join stringsprint(greeting + name)

Get one letter of a string

name[0]

Length of a string len(name)

Create an empty list menu = list()

Add an item to end of list

menu.append(thing)

Page 9: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

More Complex Commands Here are some more complex commands

Command Python

How many items on list?

len(menu)

Value of 5th item on list

menu[4]

Delete 2nd item on list

del menu[1]

Is item on a list?if ‘olives’ in menu: print(‘Oh no!’)

Command Python

Turtle graphics

from turtle import *clear()pendown()forward(100)right(90)penup()

Page 10: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Code & Shell Windows

Within the IDE there are two windows to choose from: the code window (used to write and save programs) and the shell window (run the Python instructions)

*Programs must always be saved before they can be run

Enter a program into the code window, save it and run…

#Simple Matha = 10b = 4print(a + b)print(a - b)

Page 11: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Simple Maths: Code

In the code window a simple code can be used to add or subtract some numbers

Page 12: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Simple Maths: Shell

In the shell window the code is run, however Python can also understand commands that are typed within the shell window – they run as soon as they are typed

This features allows testing: it gives an immediate response…

Page 13: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

More Turtle

Run the turtle square program

#Turtle Squarefrom turtle import *forward(100)left(90)forward(100)left(90)forward(100)left(90)forward(100)left(90)

done() *add this new line to keep the screen open

Using this basic code see if you can get turtle to draw some other shapes (and with some other pen colours / pen sizes)…

Remember, save each new shape as something specific!

Page 14: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Colour

Colour can be chosen before the line is drawn using the color command…

color(“red”) color(“blue”) Etc…

Make a multi-coloured square…

Page 15: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Colour

#Turtle Squarefrom turtle import *color("red")forward(100)left(90)color("blue")forward(100)left(90)color("green")forward(100)left(90)color("yellow")forward(100)left(90)done()

This will change the colour of the pen before each forward move…

Page 16: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Pensize

#Turtle Squarefrom turtle import *pensize(5)color("red")forward(100)left(90)color("blue")forward(100)left(90)color("green")forward(100)left(90)color("yellow")forward(100)left(90)done()

pensize(3) > this makes the pen thicker

Page 17: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Turtle Triangle

Make turtle produce a triangle...

The turtle square program may help…

#Turtle Squarefrom turtle import *forward(100)left(90)forward(100)left(90)forward(100)left(90)forward(100)left(90)done()

Page 18: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Turtle Triangle

For turtle triangle it is important to work out your angles!

#Turtle Trianglefrom turtle import *forward(50)left(120)forward(50)left(120)forward(50)done()

Page 19: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Turtle Friends

Try and have two turtles!

You’ll need to assign them names using the command: name = Turtle()

See if you can have one draw a triangle then go to the side, followed by another starting in the same place drawing a square…

Page 20: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Turtle Friends

#Turtle Trianglefrom turtle import *david = Turtle()david.color("lightgreen")david.pensize(5)steven = Turtle()steven.color("hotpink")steven.pensize(5)david.forward(80)david.left(120)david.forward(80)david.left(120)david.forward(80)david.left(120)david.right(180)david.forward(80)steven.forward(50)steven.left(90)steven.forward(50)steven.left(90)steven.forward(50)steven.left(90)steven.forward(50)steven.left(90)done()

Page 21: Noadswood Science, 2014.  To understand the flow procedure when writing programs Thursday, January 15, 2015.

Turtle Shapes

See what other turtle shapes and designs you can make…


Recommended