Download presentation
Presentation is loading. Please wait.
Published byJayden Whorton Modified over 9 years ago
1
Noadswood Science, 2014
2
To understand the flow procedure when writing programs Thursday, January 15, 2015
3
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
4
Ghosts #Ghost Game from random import randint print('Ghost Game') feeling_brave = True score = 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 + 1 print('Run away!') print('Game over! You scored', score) input("\n\nPress the enter key to exit.")
5
Ghosts #Ghost Game from random import randint print('Ghost Game') Output > print is used to display information feeling_brave = True score = 0 Processing > variables keep track of the score while 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 + 1 print('Run away!') print('Game over! You scored', score) input("\n\nPress the enter key to exit.")
6
Simple Commands Here are some basic commands that will be useful when using Python… CommandPython Run program“Run” menu Stop program“CTRL-C” in shell Text to screenprint(‘Hello!’) Set variable to numbermagic_number = 123 Set variable to text stringword = ‘Noadswood’ Read text from keyboard into a variable age = input(‘age?’) print(‘I am ‘ + age) CommandPython Add a number to a variable cats = cats + 1 Adda + 2 Subtracta - 2 Multiplya * 2 Dividea / 2 Forever loop while True: jump ()
7
Simple Commands Here are some basic commands that will be useful when using Python… CommandPython Loop 10 times for i in range (10): jump () Is equal to?a == 2 Is less than?a < 2 Is more than?a > 2 NOTnot CommandPython ORor ANDand If then if a == 2: print(‘Hello!’) If then else if a == 2: print(‘Hello!’) Else print(‘Goodbye!’)
8
More Complex Commands Here are some more complex commands CommandPython Loops with conditions while roll != 6: jump () Wait from time import sleep sleep(2) Random numbers from random import randint roll = 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) CommandPython Call a function or subprogram greet(‘chicken’) Join stringsprint(greeting + name) Get one letter of a stringname[0] Length of a stringlen(name) Create an empty listmenu = list() Add an item to end of listmenu.append(thing)
9
More Complex Commands Here are some more complex commands CommandPython How many items on list?len(menu) Value of 5 th item on listmenu[4] Delete 2 nd item on listdel menu[1] Is item on a list? if ‘olives’ in menu: print(‘Oh no!’) CommandPython Turtle graphics from turtle import * clear() pendown() forward(100) right(90) penup()
10
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 Math a = 10 b = 4 print(a + b) print(a - b)
11
Simple Maths: Code In the code window a simple code can be used to add or subtract some numbers
12
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…
13
More Turtle Run the turtle square program #Turtle Square from 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!
14
Colour Colour can be chosen before the line is drawn using the color command… color(“red”) color(“blue”) Etc… Make a multi-coloured square…
15
Colour #Turtle Square from 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…
16
Pensize #Turtle Square from 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
17
Turtle Triangle Make turtle produce a triangle... The turtle square program may help… #Turtle Square from turtle import * forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) left(90) done()
18
Turtle Triangle For turtle triangle it is important to work out your angles! #Turtle Triangle from turtle import * forward(50) left(120) forward(50) left(120) forward(50) done()
19
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…
20
Turtle Friends #Turtle Triangle from 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()
21
Turtle Shapes See what other turtle shapes and designs you can make…
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.