Python第九章实验报告
一、实验对象:《零基础学Python》第九章异常处理及程序调试的实例
二、实验环境:IDLE Shell 3.9.7
三、实验目的:了解和掌握常用的异常处理语句
四、实验过程:
- 实例01 模拟幼儿园分苹果

点击查看代码
def division():
'''功能:分苹果'''
print("\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__':
division()
、
运行结果:


- 实例02 模拟幼儿园分苹果(除数不能为0)

点击查看代码
def division():
'''功能:分苹果'''
print("\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个小朋友分!")
、
运行结果:

- 实例03 模拟幼儿园分苹果(每个人至少分到一个苹果)

点击查看代码
def division():
'''功能:分苹果'''
print("\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:
print("\n出错了~_~——",e)
、
运行结果:

- 实例04 模拟幼儿园分苹果(应用断言调试)

点击查看代码
def division():
'''功能:分苹果'''
print("\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()
、
运行结果:

Python第九章实验报告的更多相关文章
- 20201123 实验二《Python程序设计》实验报告
20201123 2020-2021-2 <Python程序设计>实验报告课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验日期 ...
- 20212115 实验二 《python程序设计》实验报告
实验二 计算器设计 #20212115 2021-2022-2 <python程序设计> 实验报告二 课程: 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号: ...
- 20184302 实验三《Python程序设计》实验报告
20184302 2019-2020-2 <Python程序设计>实验3报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:20184302 实验教师 ...
- 20201123 实验三《python程序设计》实验报告
20201123 2020-2021-2 <python程序设计>实验三报告 课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验 ...
- 20201123 实验一《Python程序设计》实验报告
20201123 2020-2021-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级:2011班 姓名:晏鹏捷 学号:20201123 实验教师: ...
- 20202127 实验二《Python程序设计》实验报告
20202127 2021-2022-2 <Python程序设计>实验二报告 课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强 ...
- 20202127 实验一《Python程序设计》实验报告
20202127 2022-2022-2 <Python程序设计>实验一报告课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强实 ...
- 20212115 实验三 《python程序设计》实验报告
实验报告 20212115<python程序设计>实验三报告 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号:20212115实验教师:王志强老师实验日期:2 ...
- 20212115朱时鸿实验一《python程序设计》实验报告
------------恢复内容开始------------ #学号20212115 <python程序设计>实验一报告 课程: <python程序设计> 班级:2121 姓名 ...
- 2019JAVA第九次实验报告
Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.8 评分等级 课程总结 1.实验代码 package Domon8; import java.awt. ...
随机推荐
- js内置方法
数组: 1.push()数组最后添加元素,pop()数组删除最后一个: unshift()数组开头添加元素,shift()删除数字第一个: 注意:push()和unshift()可以添加 ...
- 1996. 游戏中弱角色的数量 (Medium)
问题描述 1996. 游戏中弱角色的数量 (Medium) 你正在参加一个多角色游戏,每个角色都有两个主要属性: 攻击 和 防御 .给你一个二维整数数组 properties ,其中 properti ...
- JS学习- Canvas - 遮盖组合
Compositing 组合 globalCompositeOperation这个属性设定了在画新图形时采用的遮盖策略,其值是一个标识12种遮盖方式的字符串. 值 描述 图示 source-over ...
- JDK8的异步处理方式-CompletableFuture的使用
一.背景 jdk8中加入了实现类CompletableFuture,用于异步编程.底层做任务使用的是ForkJoin, 顾名思义,是将任务的数据集分为多个子数据集,而每个子集,都可以由独立的子任务来处 ...
- IDEA给【类】和【方法】设置作者和日期等注释
https://blog.csdn.net/m0_61933976/article/details/127021176 一.在Java类的开头自动注释作者名字和日期等信息 这样以后只要我们创建一个类, ...
- python调用lua脚本
目录 lua代码 python代码 lua代码 入口函数是必须要填的 function test1(params) return 'test1:'..params end function test2 ...
- Vmware 虚拟机Ubuntu系统,解决忘记用户名和密码解决办法
1.在开机界面按住shift,会加载grub的启动界面,找到Advaced options for Ubuntu选项.按"e" 进入编辑模式. 2.光标移动至ro,改为rw,(Li ...
- 049_Search Lookup (二)
其实就是 在父object中 设置,search setting 中选中 enhanced lookup, and select the dialoge & Filter 默认looukp搜 ...
- Authentication is required to set the network proxy
在使用VNC访问集群的时候,总是弹出"Authentication is required to set the network proxy used for downloading pac ...
- [OC] APP唤醒,URL Scheme,工程中的 URL Types 和 LSApplicationQueriesSchemes
1.网页唤醒APP: 假设我们有一个APP,名字叫做 "APP甲",需要通过网页唤起 APP甲,我们首先需要在 APP甲的工程文件里配置参数 URL Types: 在 info.p ...