Example: Vehicles What attributes do objects of Sedan have?

Slides:



Advertisements
Similar presentations
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Fixing Broken Programs. How do you figure out what’s wrong? Look carefully at the error message. Locate the error – Most messages have line numbers –
Python, Part 2. Python Object Equality x == y x is y In Java: (x.equals(y)) (x == y)
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
General Programming Introduction to Computing Science and Programming I.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Functions Chapter 4 Python for Informatics: Exploring Information
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Let’s get started!.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
CS 127 Exceptions and Decision Structures. Exception Handling This concept was created to allow a programmer to write code that catches and deals with.
Lecture 4 Python Basics Part 3.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Exception Handling How to handle the runtime errors.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
Lesson 06: Functions Class Participation: Class Chat:
Python Review 1.
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
George Mason University
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Lecture 4 Python Basics Part 3.
Python: Control Structures
CSC 131: Introduction to Computer Science
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSC115 Introduction to Computer Programming
Selection CIS 40 – Introduction to Programming in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Exception Handling.
Python’s Errors and Exceptions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Conditional Execution
Lecture 4 Python Basics Part 3.
The switch statement: an alternative to writing a lot of conditionals
(Oops! When things go wrong)
Input from the Console This video shows how to do CONSOLE input.
Python programming exercise
Python for Informatics: Exploring Information
Introduction to Value-Returning Functions: Generating Random Numbers
Advanced Python Concepts: Exceptions
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Advanced Python Concepts: Exceptions
By Ryan Christen Errors and Exceptions.
Exception Handling COSC 1323.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Python Exceptions and bug handling
More Basics of Python Common types of data we will work with
CIS 110: Introduction to Computer Programming
Python Reserved Words Poster
Dealing with Runtime Errors
Presentation transcript:

Example: Vehicles What attributes do objects of Sedan have? What attributes do objects of Truck have?

Design Exercise Take out your computer Write the code for class Vehicle and its subclasses Car and Truck in a file named vehicle.py Write the code in a separate file named vehicle_app.py for testing the Vehicle class that creates a few Car and Truck objects and prints their information.

Intro to Computer Science II Modules and Exceptions 3

“Exceptions” in Python Look back the example we had last time. num_students = int(input(‘Enter the number of students : ‘)) What if we typed a non-numerical input?

When “Exception” Happens Python will complain and stop execution of the program What if we want to handle the case(s) ourselves so we can control the program execution? In the example above, we’d like to ask the user to try again if the input is wrong. For example, we want the user to input a numerical value, we can also require a specific value range!

Python Defined Exceptions num = 0 while True: try: v = input("Enter a number: ") num = int(v) break except Exception : print('Input error ' + v) print('value of input ' + str(num))

Find the Type of Exceptions num = 0 while True: try: v = input("Enter a number: ") num = int(v) break except Exception as ex: # if v is a non-numeric string, the type of exception is "ValueError“ print('Input error ' + v + ' exception type ' + type(ex).__name__) print('value of input ' + str(num))

Catch a Specific Exception num = 0 while True: try: v = input("Enter a number: ") num = int(v) break except ValueError: # if v is a non-numeric string, the type of exception is "ValueError“ print(‘Value Error’) print('value of input ' + str(num))

User Defined Exceptions In the above example, we used Python defined Exception or ValueError exception. There are many pre-defined exceptions https://docs.python.org/3/library/exceptions.html There are occasions in which the programmers want their own exceptions. For example, we want to control the range of input, in addition to the type being int.

Try 1: use conditions Get out your computer, write a Python program segment based on the program in the previous example to enforce the range of input values. Let’s try to use conditions first.

Try 1: use conditions num = 0 while True: try: v = input("Enter a number: ") num = int(v) if num >= low_limit and num <= hi_limit: break else: print(‘Value out of range.’) except ValueError: # if v is a non-numeric string, the type of exception is "ValueError“ print(‘Value Type Error’) print('value of input ' + str(num))

Try 2: define your own exception class UserException(Exception): def __init__(self): self.__name__ = ‘MyException’ def __str__(self): return ‘Raise ‘ + self.__name__ while True: try: value = input('Type something : ') if value == 'exit': break except UserException: print('Try again ...')

Your Exercise Define two Python exception classes to handle the cases when a value is out of range. One is named “ValueTooSmall” The other is “ValueTooLarge” Use these two exception classes to enforce that a user must type in an integer in a given range. Use the two user-defined exceptions in program “limit_range.py”