noadswood science, 2014. to understand the flow procedure when writing programs thursday, january...
TRANSCRIPT
Noadswood Science, 2014
To understand the flow procedure when writing programs
Tuesday, April 11, 2023
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
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.")
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.")
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 ()
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!’)
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)
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()
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)
Simple Maths: Code
In the code window a simple code can be used to add or subtract some numbers
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…
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!
Colour
Colour can be chosen before the line is drawn using the color command…
color(“red”) color(“blue”) Etc…
Make a multi-coloured square…
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…
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
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()
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()
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…
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()
Turtle Shapes
See what other turtle shapes and designs you can make…