Top Banner
ADVANCED CODING SESSION 4 Royston Shufflebotham
16

ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

Dec 17, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

ADVANCED CODINGSESSION 4Royston Shufflebotham

Page 2: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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?

Page 3: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

TODAY

• Scratch• Nothing!

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

Page 4: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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> ?

Page 5: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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…

Page 6: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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'

Page 7: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

TURTLES ALL THE WAY DOWN!

• Python Turtle Graphics

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

Page 8: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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.

Page 9: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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)

Page 10: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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’)

Page 11: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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

Page 12: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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

Page 13: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

THAT PROGRAM…

Page 14: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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

Page 15: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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

Page 16: ADVANCED CODING SESSION 4 Royston Shufflebotham. WHILST WE’RE WAITING FOR PEOPLE TO ARRIVE… Start up Python (IDLE from the Windows menu) Try out things.

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??