博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lecture 4.2 异常
阅读量:6997 次
发布时间:2019-06-27

本文共 1238 字,大约阅读时间需要 4 分钟。

1  try 

Suppose we rewrite the FancyDivide function to use a helper function.

def FancyDivide(list_of_numbers, index):   denom = list_of_numbers[index]   return [SimpleDivide(item, denom)               for item in list_of_numbers]
def SimpleDivide(item, denom):   return item / denom

This code raises a ZeroDivisionError exception for the following call: FancyDivide([0, 2, 4], 0)

Your task is to change the definition of SimpleDivide so that the call does not raise an exception. When dividing by 0, FancyDivide should return a list with all 0 elements. Any other error cases should still raise exceptions. You should only handle the ZeroDivisionError.

#define the SimpleDivide function heredef SimpleDivide(item, denom):    try:        return item / denom        # catch a division by zero and return 0    except ZeroDivisionError, e:        return 0

e:表示的是异常的msg

2 assert

def Normalize(numbers):    max_number = max(numbers)    assert(max_number != 0), "Cannot divide by 0"    for i in range(len(numbers)):        numbers[i]  /= float(max_number)        assert(0.0 <= numbers[i] <= 1.0), "output not between 0 and 1"    return numbers

assert(max_number != 0), "Cannot divide by 0"   #如果max_number != 0不成立 则抛出"Cannot divide by 0"  否则continue

转载地址:http://ojdvl.baihongyu.com/

你可能感兴趣的文章
IE自动化 二(判断IP所在地)
查看>>
select下拉框多选 和回显
查看>>
苹果允许Flash程序在iPad和iPhone中使用
查看>>
一起谈.NET技术,XML与DataSet对象的关系
查看>>
艾伟_转载:【译】12个asp.net MVC最佳实践
查看>>
MySQL索引
查看>>
flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table
查看>>
每个势利鬼都有一副奴才相
查看>>
LINQ to Object的一个例子
查看>>
在CI框架中如何实现伪静态
查看>>
ORACLE Postgresql中文排序
查看>>
UBOOT到内核到文件系统设置需要注意点
查看>>
卡尔曼滤波简介——4.方差比较
查看>>
mysql -- 预处理语句
查看>>
Silverlight如何调用淘宝API
查看>>
ESP8266- AP模式的使用
查看>>
hdu 1503 LCS输出路径【dp】
查看>>
博客园开张第一天
查看>>
java绘图
查看>>
The Semantic Web, Linked Data and Open Data
查看>>