28
Noadswood Science, 2014

Noadswood Science, 2014. To set out and solve an identified problem Saturday, September 19, 2015

Embed Size (px)

Citation preview

Page 1: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Noadswood Science, 2014

Page 2: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

To set out and solve an identified problem

Wednesday, April 19, 2023

Page 3: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Drawing Machine

In this mini project your task is to create a program to achieve the following: -

A program which draws some shapes (or instructions given by the user)

A user inputs a set of simplified instructions (distance / direction) into a program with good usability (i.e. a pop-up window)

The program takes these instructions and processes them

A drawing is produced on screen

There are options for the user to draw new shapes or quit the program

Page 4: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Drawing Machine

The program Drawing Machine will turn a string of simple instructions into turtle commands to draw different shapes

There are three parts to the program Function 1 > turtle_controller Function 2 > string_artist Function 3 > a user interface

Function 1 (turtle_controller) takes simple commands from the user and turns it into a turtle command

Function 2 (string_artist) takes the strings a user has inputted and splits them into smaller units which are then fed to the turtle_controller

Function 3 (a user interface) allows the user to input (which are fed to string_artist)

Page 5: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Drawing Machine

In order to write our program which can draw any shape it is useful to choose an initial shape (this will allow us to test if our program works as we already know the outcome of the shape we are after)

We can start with a familiar house shape using turtle –

In turtle draw this shape using the following commands: turtle import *; reset(); left(xx); forward(xx); right(xx) etc... where (xx) is an integer

Page 6: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Test Shape

#Turtle Housefrom turtle import* < loads turtlereset() < resets turtle to centre of screenleft(90)forward(100) < moves turtle forward by 100right(45)forward(70)right(90) < turtle turns by 90 degrees to the rightforward(70)right(45)forward(100)right(90)forward(100)

Page 7: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Flow Chart

Coders often plan programs on paper – one of the easiest ways is to use a flow chart showing the steps and decisions the program will need to undertake

In a flow chart squares represent actions whilst diamonds represent decisions

Action

Decision

Page 8: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Flow Chart

Produce a flow chart showing the plan for the turtle_controller function

It will need to take an input “do” which will be a letter such as F, B, R and L and an input “val” which will be a number such as 45, 90 and 100 and turn them into a turtle command

For example, an input of “F100” would need to be turned into the “do” command forward and the “val” command 100,

The flow chart will also need to include an error function – needed if the program doesn’t recognise a letter inputted

The turtle_controller will use the following characters which will be turned into different commands: N (reset); U (pen up); D (pen down); F (forward); B (backward); R (turn right); and L (turn left)

Page 9: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Flow Chart

inputs “do” and “val”

forward(val)

right(val)

penup()

report unknown command

return from function

do == U ?

do == R ?

do == F ?

Page 10: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Flow Chart

inputs “do” and “val”

forward(val)

right(val)

penup()

report unknown command

return from function

do == U ?

do == R ?

do == F ?

Each commands has two variables “do” (string) for what to do and “val” (integer) how far to do it

If “do” = F then move turtle forward

If “do” = R then turn turtle right

If “do” = U then “penup()” which stops turtle drawing

Once the command is finished return to the main program

If “do” isn’t a character recognised then report an error

“do” isn’t “R” – is it

“U” ?

“do” isn’t “F” – is it

“R” ?

Function decides if “do” is a

character it recognises

Page 11: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Turtle Controller

The first part of the program is a function that moves the turtle – one command at a time (as planned out in the flow chart previously)

The “do” and “val” functions need to be converted into movement commands within turtle

Write a piece of code which incorporates the following criteria Turtle is imported Statements are defined “def” for inputs (“do” and “val”) Decisions “if”, “elif” and “else” statements are used if the user

inputs “F” (forward); “B” (backwards); “R” (right); “L” (left); “U” (pen up); “D” (pen down); “N” (reset); and any other input prints out an error statement

All characters are converted to upper case resolves some errors!

Page 12: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Turtle Controller

#Turtle Controllerfrom turtle import* < loads turtledef turtle_controller(do, val): < defines “do” and “val” as inputs for the function do = do.upper() < converts all letters in “do” to upper case if do == 'F': forward(val) < function changes “do” value “F” into command forward elif do == 'B': < function checks the “do” character against all defined backward(val) elif do == 'R': right(val) elif do == 'L': left(val) elif do == 'U': penup() < turtle stops drawing elif do == 'D': pendown() < turtle starts drawing elif do == 'N': reset() < command resets turtle’s position to centre of screen else: print('Unrecognised command') < if “do” value cannot be recognised message appears

Page 13: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Pseudocode

Programming can be planned using pseudocode (fake code) – it allows you to write your ideas in the style of how you want the program to run without actually ‘knowing’ the programming language

Clear coding is vital – it is vital that people can follow what you’ve done. It is important to make the code as easy to understand as possible, using the following techniques: - Use functions – breaking code into smaller chunks (where each

function completes a task in the program) Name variables and functions – make them clear (e.g.

“age_in_years” is more obvious than “aiy” Comment - #explain what is happening so when you come to

read back the code it makes more sense Symbols – don’t use ones that can be confused with others

(e.g. capital O looks like zero (0) and lowercase l looks like an capital I or 1)!

Page 14: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Pseudocode

It is now time to plan the string_artist – this function will take a string of several “do” and “val” command inputs and break them into pairs made up of a letter and a number. It can then pass these pairs to the turtle controller, one at a time

For example the following input might be given

F100-R90-F50-R45

The function of the string_artist will be to take that input and process it into the following

‘F’ 100 ‘R’ 90 ‘F’ 50 ‘R’ 45

Organise a pseudocode to achieve the above

Page 15: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Pseudocode

Basic functions needed: get a string from an input; split; what to do with errors; “do” command is where and what; “val” command is where and what; pass to turtle…

A flow chart can really help simplify what you want to achieve (and helps visualise it)

xyz

xyz

xyz

xyz

xyz

Page 16: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Pseudocode

function string_artist(input – program as string):

split program string into list of commands

for each command in list

check it is not blank

if it is blank move onto the next item

command type is the first letter

if followed by more characters

turn them into a number

call turtle_controller(command type, number)

Function takes a string of commands by user

Splits strings into a list of separate commands

Blank commands are skipped

First character is a “do” command

Following characters are

“val” commands but these must be turned into

numbersCommand passed to turtle

Page 17: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

String Artist

The pseudocode set out plans a function known as the string_artist – this is planned to turn a string of values into single commands that are sent to the turtle controller

This pseudocode needs to be converted into real Python code…

The “split()” function splits a string into a list of smaller strings where each break point is marked with a character (e.g. “-”)

program = ‘N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100’cmd_list = program.split(‘-’)cmd_list

When run, the “split()” function breaks the string down as follows: -

‘N’, ‘L90’, ‘F100’, ‘R45’, ‘F70’, ‘R90’, ‘F70’, ‘R45’, ‘F100’, ‘R90’, ‘F100’

Page 18: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

String Artist

#String Artistdef string_artist(program): cmd_list = program.split('-') for command in cmd_list: cmd_len = len(command) if cmd_len == 0: continue cmd_type = command[0] num = 0 if cmd_len > 1: num_string = command[1:] num = int(num_string) print(command, ':', cmd_type, num) turtle_controller(cmd_type, num)

Whenever “-” occurs split string

Loop through the list of strings, making each item a

single turtle command

If the length of the command is 0 (i.e. a blank command) then skip to

next string

Finds length of command string

Take the first character of the command and set it as the

command typeTakes remaining characters

from the command (by cutting off the first)

Prints the commands on the screen so you

can see what the code is doing

Converts the string characters into numbers

Passes command to

turtle

Page 19: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Input > Process

The user input might resemble something as follows: -

‘N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100’

The program then converts it into the following: -

N : N 0 < resets screen and turtle put back at 0L90 : L 90F100 : F 100R45 : R 45F70 : F 70R90 : R 90F70 : F 70R45 : R 45F100 : F 100R90 : R 90F100 : F 100

Forward 100

Rotate 45

Page 20: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

User Interface

The drawing machine needs an interface to make it easier to use – this will let the user enter a string from the keyboard to tell the program what to draw…

A pop-up window is created with basic instructions (and a while loop allows multiple entries)

Page 21: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

User Interface

The user window makes the turtle far easier to control, and it also means the program does not have to be restarted for new pictures (typing ‘N’ runs the command to start a new drawing)

The window will pop-up over the turtle window ready for the user to type information in (with an option to ‘OK’ when the user wants to run the program)

Page 22: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

User Interface

#User Interfaceinstructions = '''Enter a program for the turtle:e.g. F100-R45-U-F100-L45-D-F100-R90-B50N = New drawingU = Pen upD = Pen downF100 = Forward 100B50 = Backward 50R90 = Right turn 90 degreesL45 = Left turn 45 degreesChoose a numerical value for movement/turning'''screen = getscreen()while True: t_program = screen.textinput('Drawing Machine', instructions) print(t_program) if t_program == None or t_program.upper() == 'END': break string_artist(t_program)

Triple quotes inform python everything including line breaks

are part of the same string

What to display in the pop-up window

This line stops the program if the user

types “END” or presses the cancel

button

Passes string to the string_artist function

Page 23: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

User Interface

The user interface gives the user information and advice with what to enter making the program more useable…

getscreen() is the function called to produce the pop-up

Page 24: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Error

The program uses the command print(‘Unrecognised command’) if the user enters a value that the function cannot recognise (although in this early stage it can still be broken)

Page 25: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

House

The Drawing Machine program takes instructions and passes them into the turtle program to draw – for example the instructions entered by a user below with draw a simple outline of a house

N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100

Page 26: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

House Window

However, using the U and D commands the pen will lift, allowing for much more complicated shapes to be drawn…

N-L90-F100-R45-F70-R90-F70-R45-F100-R90-F100-B10-U-R90-F10-D-F30-R90-F30-R90-F30-R90-F30

Page 27: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Owl Face

Very complex shapes can be made with more and more elaborate instructions…

N-F100-L90-F200-L90-F50-R60-F30-L120-F30-R60-F40-R60-F30-L120-F30-R60-F50-L90-F200-L90-F100-L90-U-F150-L90-F20-D-F30-L90-F30-L90-F30-L90-F30-R90-U-F40-D-F30-R90-F30-R90-F30-R90-F30-L180-U-F60-R90-D-F40-L120-F40-L120-F40

Page 28: Noadswood Science, 2014.  To set out and solve an identified problem Saturday, September 19, 2015

Forest

Using the program create your own shapes (or even have the computer randomly draw a number of ‘trees’ to produce a computerised ‘forest’ (different every time…))