Can You Compute?
  • HOME
    • Yr 6
    • Revision
    • Class of 2017
    • COVER LESSON FB
    • Yr 10: Program of Study 2016
    • Yr 11:Coming soon
    • Student Showcase
  • KS3
    • Term 6: Databases
    • Term 5 Scratch
    • Term 4 Websites
    • Term 3 Spreadsheets
    • Term 2: Binary & Algorithms
    • Term 1: Basics and Hardware
  • J277/01 NEW
    • EPIC FAILS
    • Intro Lessons
    • 1.1 System Architecture >
      • 1.1.1: The CPU
      • 1.1.1: Parts of the CPU
      • 1.1.2: The CPU in action
      • CPU Recap Lesson
      • Little Man Computer
      • 1.1.3: Embedded Systems
      • RISC & CISC
      • End of Unit Exam
    • 1.2 Memory and Storage >
      • 1.2.1: Primary Storage (Memory)
      • Intro to Units of Memory
      • 1.2.2 Secondary Storage
      • How stuff works
    • 1.2.3 Units & Data Capacity
    • 1.2.4 Data Storage >
      • 1.2.4 Binary Conversion
      • 1.2.4 Binary Addition
      • L4: Hexadecimal
      • L5: Recap
      • L6: Characters
      • L7: Images
      • L8: SenseHAT images
      • L9: Sound & Sampling
      • L10: Code Sound
      • L11: Compression Part 1
      • L12: Compression Part 2 >
        • Compression
      • What do you know?
  • J276/02 old
    • Unit 2 Overview
    • 2.1 Algorithms >
      • L1: Intro to Algorithms
      • L2: The Knight's Tour
      • L3: Bresenham
      • L4: Searching Algorithms
      • L5: Bubble Sort
      • L6: Insertion Sort
      • L7: Merge
      • L8: Sorting Recap
      • L9: Creating Algorithms
      • L10: Pseudocode
      • L11: Interpreting Algorithms
      • L12: End of Unit Test
    • 2.2 Programming Techniques >
      • Coding Intro
      • L1: Programming Concepts
      • L2: Sequence, Selection & Iteration
      • L3: SSI Practice
      • L4: String Manipulation
      • L5: Secret Codes
      • L6: File Handling
      • L7: Use of records to store data
      • L8: SQL
      • L9: SQL Python
      • L10 ,11, 12: Arrays
      • L13: Functions
      • L14: Data Types
      • L15: Casting
      • L16 -17: Test and Code
    • 2.3 Robust Programs >
      • L1 : Defensive Design
      • L2 : Testing Programs
    • 2.4 Computational Logic >
      • L1: Comp. Logic 1
      • L2: Comp. Logic 2
      • L3: Operators (Python)
    • 2.5 Translators & Language >
      • L1: Types of Languages
      • L2: Translators
  • Yr 9
    • Year End Examination
    • iMedia 5: Comics
    • iMedia 4: School Brochure
    • iMedia 3: Presentations
    • iMedia 2: Review and Mail Merge
    • iMedia 1: IT Letter Writing
    • L10: Intro to iMedia
    • L9 Building a Computer
    • L8: Buying a House
    • L7: Logic Gates
    • L6: Binary
    • L5: CS Alan Turing
    • L4: Spreadsheets
    • L3: JS in an Hour!
    • L2: Intro to Coding
    • L1: CS Compression
  • Py
    • Coding Concepts
    • Writing your first Program
    • Python Pages
    • Python Playground
    • Strings
    • Python Operators
    • Functions
    • Conditions
    • Try and Except
    • Break and Continue
    • Odd codes
  • Py +
    • Looping
    • Why does my code not work?
    • Errors
    • Validation
    • Lists
    • Lists again
    • Looking for data in String
    • Writing to Files
    • Read, Write & Sorting Data
  • CODING
    • Magic Python
    • Alan Turing
    • Brian the Turtle
    • Grade Calculator
    • Nested IF Hotels
    • Vending Machine
    • A Looping Menu
    • For
    • Christmas Coding
    • JS in an Hour!
    • CSS + HTML
    • Medical Claims
    • Hour of Code 2015
  • Contact
    • Dictionaries - coming soon
    • L2:
  • Squid Game
  • Reflectico

TRY AND EXCEPT


Lesson Aims:
  • know what Try and Except is 
  • Apply the code correctly 
  • Be able to explain why and when it is used.
Picture

Starer: Find my errors:
  1. Look at the program on the left, it is based on an old nursery rhyme
  2. What does the program do?
  3. What should it do?
  4. There are 6 errors, correct them and make the program run
Lesson Introduction: An Example of Try and Except

Sometimes you will have a section of code that is working and then you will introduce a new section which makes it fail.  This can be very frustrating indeed!  To resolve this you can use try and except, try an section of code and if it fails then except it. Note that this is EXCEPT not accept.
  1. Look up the difference between accept and except
  2. Look at the code below and run it, it does not print line 7, why?
  3. What is the error?
  4. Without deleting the Try and Except, edit the program to print line 7.
  5. Look at the algorithmic representation of the code and discuss. 
Picture
Picture
Part 2: Another Example:
  1. Run the code below and enter a number -  what happens?
  2. Run it again an enter text / letters -  what happens, why?
  3. On line 8 add the code line, print number, run the code what happens.
Part 3: Your turn
  1. Look at the code below - use a try and except to check that a number has been entered, if it has then print nice work, if not a number then print an error message of your choice.
  2. Check out the answer here
Part 4: Pay Day
  1. Look at the program below that works out your pay.
  2. Try it out, Enter Hours: 45, Enter Rate: 10, Pay: 450
  3. Rewrite the computation so that the employee gets paid 1.5 (15 punds) for every hour worked above 40 hours.
  4. How much do they get paid, check with your partner / teacher
  5. Now re-write your pay program using try and except so that your program handles non-numeric inputs gracefully.
  6. For example,  Enter Hours: 20, Enter Rate: nine, Error, please enter numeric input
  7. Enter Hours: forty, Error, please enter numeric input
What have you learnt today?
  1. Tell another friend what you found hard and why
  2. What three new skills have you learnt
  3. Describe two programs where you might use Try and Except



Powered by Create your own unique website with customizable templates.
  • HOME
    • Yr 6
    • Revision
    • Class of 2017
    • COVER LESSON FB
    • Yr 10: Program of Study 2016
    • Yr 11:Coming soon
    • Student Showcase
  • KS3
    • Term 6: Databases
    • Term 5 Scratch
    • Term 4 Websites
    • Term 3 Spreadsheets
    • Term 2: Binary & Algorithms
    • Term 1: Basics and Hardware
  • J277/01 NEW
    • EPIC FAILS
    • Intro Lessons
    • 1.1 System Architecture >
      • 1.1.1: The CPU
      • 1.1.1: Parts of the CPU
      • 1.1.2: The CPU in action
      • CPU Recap Lesson
      • Little Man Computer
      • 1.1.3: Embedded Systems
      • RISC & CISC
      • End of Unit Exam
    • 1.2 Memory and Storage >
      • 1.2.1: Primary Storage (Memory)
      • Intro to Units of Memory
      • 1.2.2 Secondary Storage
      • How stuff works
    • 1.2.3 Units & Data Capacity
    • 1.2.4 Data Storage >
      • 1.2.4 Binary Conversion
      • 1.2.4 Binary Addition
      • L4: Hexadecimal
      • L5: Recap
      • L6: Characters
      • L7: Images
      • L8: SenseHAT images
      • L9: Sound & Sampling
      • L10: Code Sound
      • L11: Compression Part 1
      • L12: Compression Part 2 >
        • Compression
      • What do you know?
  • J276/02 old
    • Unit 2 Overview
    • 2.1 Algorithms >
      • L1: Intro to Algorithms
      • L2: The Knight's Tour
      • L3: Bresenham
      • L4: Searching Algorithms
      • L5: Bubble Sort
      • L6: Insertion Sort
      • L7: Merge
      • L8: Sorting Recap
      • L9: Creating Algorithms
      • L10: Pseudocode
      • L11: Interpreting Algorithms
      • L12: End of Unit Test
    • 2.2 Programming Techniques >
      • Coding Intro
      • L1: Programming Concepts
      • L2: Sequence, Selection & Iteration
      • L3: SSI Practice
      • L4: String Manipulation
      • L5: Secret Codes
      • L6: File Handling
      • L7: Use of records to store data
      • L8: SQL
      • L9: SQL Python
      • L10 ,11, 12: Arrays
      • L13: Functions
      • L14: Data Types
      • L15: Casting
      • L16 -17: Test and Code
    • 2.3 Robust Programs >
      • L1 : Defensive Design
      • L2 : Testing Programs
    • 2.4 Computational Logic >
      • L1: Comp. Logic 1
      • L2: Comp. Logic 2
      • L3: Operators (Python)
    • 2.5 Translators & Language >
      • L1: Types of Languages
      • L2: Translators
  • Yr 9
    • Year End Examination
    • iMedia 5: Comics
    • iMedia 4: School Brochure
    • iMedia 3: Presentations
    • iMedia 2: Review and Mail Merge
    • iMedia 1: IT Letter Writing
    • L10: Intro to iMedia
    • L9 Building a Computer
    • L8: Buying a House
    • L7: Logic Gates
    • L6: Binary
    • L5: CS Alan Turing
    • L4: Spreadsheets
    • L3: JS in an Hour!
    • L2: Intro to Coding
    • L1: CS Compression
  • Py
    • Coding Concepts
    • Writing your first Program
    • Python Pages
    • Python Playground
    • Strings
    • Python Operators
    • Functions
    • Conditions
    • Try and Except
    • Break and Continue
    • Odd codes
  • Py +
    • Looping
    • Why does my code not work?
    • Errors
    • Validation
    • Lists
    • Lists again
    • Looking for data in String
    • Writing to Files
    • Read, Write & Sorting Data
  • CODING
    • Magic Python
    • Alan Turing
    • Brian the Turtle
    • Grade Calculator
    • Nested IF Hotels
    • Vending Machine
    • A Looping Menu
    • For
    • Christmas Coding
    • JS in an Hour!
    • CSS + HTML
    • Medical Claims
    • Hour of Code 2015
  • Contact
    • Dictionaries - coming soon
    • L2:
  • Squid Game
  • Reflectico