defdivision():"'功能:分苹果'"print("\n==================分苹果=====================\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))
result = apple // children # 计算每人分几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:# 捕获异常
division()# 调用分苹果的函数except ZeroDivisionError:# 处理异常print("\n出错了~_~——苹果不能被0个小朋友分!")
运行结果:
如果输入苹果的个数为小数时,将直接抛出异常:
在上面的基础上再添加一个except语句,用于处理抛出ValueError异常的情况。
defdivision():"'功能:分苹果'"print("\n==================分苹果=====================\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))
result = apple // children # 计算每人分几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:# 捕获异常
division()# 调用分苹果的函数except ZeroDivisionError:# 处理异常print("\n出错了~_~——苹果不能被0个小朋友分!")except ValueError as e:# 处理ValueError异常print("输入错误:", e)# 输出错误原因
defdivision():"'功能:分苹果'"print("\n==================分苹果=====================\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))
result = apple // children # 计算每人分几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:# 捕获异常
division()# 调用分苹果的函数except ZeroDivisionError:# 处理异常print("\n出错了~_~——苹果不能被0个小朋友分!")except ValueError as e:# 处理ValueError异常print("输入错误:", e)# 输出错误原因else:# 没有抛出异常时执行print("分苹果顺利完成...")
defdivision():"'功能:分苹果'"print("\n==================分苹果=====================\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))
result = apple // children # 计算每人分几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:# 捕获异常
division()# 调用分苹果的函数except ZeroDivisionError:# 处理异常print("\n出错了~_~——苹果不能被0个小朋友分!")except ValueError as e:# 处理ValueError异常print("输入错误:", e)# 输出错误原因else:# 没有抛出异常时执行print("分苹果顺利完成...")finally:# 无论是否抛出异常都执行print("进行了一次分苹果操作")
defdivision():"'功能:分苹果'"print("\n=========================分苹果了===============\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))if apple < children:raise ValueError("苹果太少了,不够分...")
result = apple // children # 计算每个人分得到几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:# 捕获异常
division()# 调用分苹果函数except ZeroDivisionError:# 处理除零异常print("\n出错了~_~——苹果不能被0个小朋友分!")except ValueError as e:# ValueErrorprint("\n出错了 ~_~——", e)
defdivision():"'功能:分苹果'"print("\n=========================分苹果了===============\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))assert apple > children,"苹果不够分"# 应用断言调试
result = apple // children # 计算每个人分得到几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":
division()# 调用分苹果的函数
运行结果如下:
通常情况下,assert语句可以和异常处理语句结合使用,上面的代码可以改成如下:
defdivision():"'功能:分苹果'"print("\n=========================分苹果了===============\n")
apple =int(input("请输入苹果的个数:"))# 输入苹果的个数
children =int(input("请输入来了几个小朋友:"))assert apple > children,"苹果不够分"# 应用断言调试
result = apple // children # 计算每个人分得到几个苹果
remain = apple - result * children # 计算余下几个苹果if remain >0:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个,剩下", remain,"个。")else:print(apple,"个苹果,平均分给", children,"个小朋友,每人分", result,"个。")if __name__ =="__main__":try:
division()# 调用分苹果的函数except AssertionError as e:# 处理AssertionError异常print("\n输入有误:", e)
前言
队列(queue)是一种线性数据结构,队列中的元素只能先入先出(First In First Out,简称 FIFO)。队列和实际生活中的排队相对应,是一种和生活息息相关的数据结构,在很多系统中都会…