advanced coding session 4 royston shufflebotham. whilst we’re waiting for people to arrive…...

16
ADVANCED CODING SESSION 4 Royston Shufflebotham

Upload: patricia-skinner

Post on 17-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

ADVANCED CODINGSESSION 4Royston Shufflebotham

WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE…

• Start up Python• (IDLE from the Windows menu)

• Try out things from last time:• Variables

• height = 42• Arithmetic

• +, -, *, /, (, )• What doesx = 7x = x + 1do?

• **, //, % - what do they do?

TODAY

• Scratch• Nothing!

• Python• Recap: variables• Errors• Saving + Running Python Programs• Python Turtles

PYTHON - VARIABLES

• Calculating area in Python

• How old will you be when you’re 9 times the age you are now?age = 7age * 9

• What happens if you trymy age = <your age> ?

PYTHON – ERRORS

• We’re not allowed to make a variable called “my age”. (Variable names can’t contain spaces.)

• Get used to seeing errors from Python!

• Python tries to be helpful

• But errors can look a bit scary at first…

SCARY ERROR?

Traceback (most recent call last):

File "checker.py", line 341, in <module>

problems_found += process(input_file)

File "checker.py", line 321, in process

problems_found += process(full_file)

File "checker.py", line 300, in process

problems = checkFile(filename)

File "checker.py", line 248, in checkFile

parser.feed(fileContents)

File "C:\Python27\lib\HTMLParser.py", line 117, in feed

self.goahead(0)

File "C:\Python27\lib\HTMLParser.py", line 163, in goahead

k = self.parse_endtag(i)

File "C:\Python27\lib\HTMLParser.py", line 401, in parse_endtag

self.handle_endtag(elem)

File "checker.py", line 61, in handle_endtag

self.tagStack.pushh()

AttributeError: 'list' object has no attribute ‘pushh'

TURTLES ALL THE WAY DOWN!

• Python Turtle Graphics

• Like drawing commands (pen up, pen down, move, turn) in Scratch

CREATE A TURTLE

Task: Create a Turtle:

1. Typeimport turtle

2. This tells Python that we’re going to be doing turtle-y things.

3. Typet = turtle.Turtle()

( is SHIFT-9) is SHIFT-0

4. Another window appears!

5. Make the new window smaller so you can still see the ‘Shell’ window as well.

WHAAA?

• What did we just do?t = turtle.Turtle()

• What wouldt = 9have done?

• turtle.Turtle() is how we tell Python to create a new “Turtle object”

• What’s in t? (Try it)

TURTLE POWER!

Task: Get our Turtle to draw a shape by running Python commands:

1. Type these commands (and watch your turtle as you go through them):t.forward(100)t.right(120)t.forward(100)t.right(120)t.forward(100)

2. If you’d like your Turtle to be shaped differently, type this at any time:t.shape(’turtle’)

CREATING PYTHON PROGRAMS

• So far we’ve just typed commands into the “Python Shell”

• Have to type each command in when we want it

• Have to retype the whole command when we make a mistake

• We really want to work with programs we can load and save• So we can run them as many times as we like• So we can share them with others• So we can tweak them

RUNNING A SAVED PYTHON PROGRAMTask: Run an existing Python program

1. (Close the ‘Python Turtle Graphics’ window if you still have it)

2. In IDLE, open up the program called questions.py you’ll find in your folder1. File->Open2. Go to your folder and choose questions.py3. Open it but don’t make any changes yet!

3. Run->Run Module

4. Use the program in your “Python Shell” window

THAT PROGRAM…

MODIFY A PYTHON PROGRAM

Task: Modify an existing Python program

1. Change questions.py so it does a multiply (or “times”) instead of an add.a) Change what it prints at the start (“multiply” instead of “add”)b) Change what it does with the two numbers (* instead of +)

2. Run your program (with Run->Run Module)(Note: if you haven’t already saved your changes, it’ll make you save them first!)

3. Use the program in your “Python Shell” window

END OF TERM•What have we learned?• An AMAZING amount!

• In just 4 sessions:• Created our own blocks in Scratch•We can run Python• Variables• Arithmetic• Errors•Opening, Finding, Saving Files• Python Turtle Graphics

NEXT TERM

• Python• Loops• If, Functions, Classes• (Maybe?) PyGame

• Other Stuff?• Writing web pages• How computers work – what’s inside them – take one apart?• Multi-core computing – how does Google work?• Some hardware playing/electronics with Raspberry Pis?• What else??