Ch 12 test three

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

except AttributeError

An AttributeError occurs if a function does not exist in an imported module. Fill in the missing code to handle AttributeErrors gracefully and generate an error if other types of exceptions occur. import my_lib try: result = my_lib.magic() SOLUTION: print('No magic() function in my_lib.')

except

Fill in the missing code so that any type of error in the try block is handled. ages = [] prompt = "Enter age ('q' to quit):" user_input = input(prompt) while user_input != 'q': try: ages.append(int(user_input)) user_input = input(prompt) SOLUTION: print('Unable to add age.') print(ages)

False-needs to end with an error

T/F "FileNotOpen" is a good name for a custom exception class

False

T/F The following statement defines a new type of exception: def MyMultError: pass

except:

a catch-all exception handler

raise

causes immediate exit from the try block and the execution of an exception handler

custom exception type

class LessThanZeroError(Exception): def __init__(self, value): self.value = value my_num = int(input('Enter number: ')) if my_num < 0: raise LessThanZeroError('my_num must be greater than 0') else: print('my_num:', my_num)

try

describes a block of code that uses exception handling

exception handling

meaning handling exceptional conditions (errors during execution)

raise statement

try: weight = int(input('Enter weight (in pounds): ')) if weight < 0: raise ValueError('Invalid weight.') height = int(input('Enter height (in inches): ')) if height < 0: raise ValueError('Invalid height.') bmi = (float(weight) / float(height * height)) * 703 print('BMI:', bmi) print('(CDC: 18.6-24.9 normal)\n') # Source www.cdc.gov except ValueError as excpt: print(excpt) print('Could not calculate health info.\n')

exception handling

user_input = '' while user_input != 'q': try: weight = int(input("Enter weight (in pounds): ")) height = int(input("Enter height (in inches): ")) bmi = (float(weight) / float(height * height)) 703 print('BMI:', bmi) print('(CDC: 18.6-24.9 normal)\n') except: print('Could not calculate health info.\n')


Set pelajaran terkait

SIE Exam #2 (ch. 11, 12, 14, 20)

View Set

International Marketing - EXAM 2 - Chapter 10

View Set

Intro to Global Politics Exam #1

View Set

Khan Academy Programming Study List

View Set

Decision making in operations midterm word questions

View Set