一、实验对象:《零基础学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第九章实验报告的更多相关文章

  1. 20201123 实验二《Python程序设计》实验报告

    20201123 2020-2021-2 <Python程序设计>实验报告课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验日期 ...

  2. 20212115 实验二 《python程序设计》实验报告

    实验二 计算器设计 #20212115 2021-2022-2 <python程序设计> 实验报告二 课程: 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号: ...

  3. 20184302 实验三《Python程序设计》实验报告

    20184302 2019-2020-2 <Python程序设计>实验3报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:20184302 实验教师 ...

  4. 20201123 实验三《python程序设计》实验报告

    20201123 2020-2021-2 <python程序设计>实验三报告 课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验 ...

  5. 20201123 实验一《Python程序设计》实验报告

    20201123 2020-2021-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级:2011班 姓名:晏鹏捷 学号:20201123 实验教师: ...

  6. 20202127 实验二《Python程序设计》实验报告

    20202127 2021-2022-2 <Python程序设计>实验二报告 课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强 ...

  7. 20202127 实验一《Python程序设计》实验报告

    20202127 2022-2022-2 <Python程序设计>实验一报告课程:<Python程序设计>班级: 2021姓名: 马艺洲学号:20202127实验教师:王志强实 ...

  8. 20212115 实验三 《python程序设计》实验报告

    实验报告 20212115<python程序设计>实验三报告 课程:<Python程序设计>班级: 2121姓名: 朱时鸿学号:20212115实验教师:王志强老师实验日期:2 ...

  9. 20212115朱时鸿实验一《python程序设计》实验报告

    ------------恢复内容开始------------ #学号20212115 <python程序设计>实验一报告 课程: <python程序设计> 班级:2121 姓名 ...

  10. 2019JAVA第九次实验报告

    Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.8 评分等级 课程总结 1.实验代码 package Domon8; import java.awt. ...

随机推荐

  1. 原因代码: 0x2000c 关机类型: 关机

    进程 C:\Windows\system32\silsvc.exe (XTKFSERVER2019) 由于以下原因已代表用户 NT AUTHORITY\SYSTEM 启动计算机 XTKFSERVER2 ...

  2. conda出现Solving environment: failed错误

    conda在使用create新建环境和install安装时报错"Solving environment: failed" 报错截图  解决方案 1.在cmd中输入 %HOMEPAT ...

  3. Java流程控制之Scanner的进阶使用

    Scanner的进阶使用 import java.util.Scanner; public class Demo04 { public static void main(String[] args) ...

  4. Eureka服务端创建及服务注册

    一.Eureka服务端创建 1. 引入依赖:pom.xml <!-- eureka 服务端 --> <dependency> <groupId>org.spring ...

  5. SignalR《二》接着前篇的继续

    SignalR<二>接着前篇的继续 SignalR身份验证 在ChatRoomHub加上[Authorize] 这样登录了才能发送消息  using Microsoft.AspNetCor ...

  6. pg_freespacemap查看表膨胀

    pg_freespacemap模块提供一种检查自由空间映射(FSM)的手段.它提供一个名为pg_freespace的函数,或精确的说是两个重载函数.该函数在一个给定的页面或关系中的所有页面的自由空间映 ...

  7. python写入sqlserver中文乱码问题

    需求是python3开发,数据库是sqlserver,第一次用python操作sqlserver,写入数据时,中文全部变成了?? 试了pyodbc,但缺少sqlserver驱动 试了sqlStr.en ...

  8. CCIE DC Multicast Part 1.

    Hi Guys! As we all wait anxiously for the training vendors to release Rack Rentals (Come on guys! At ...

  9. mySql查询-系统公告发布接收人情况

    -- display_name NZ分部所有用户SELECT user_code,display_name FROM scy_user WHERE ou_id=1627 AND is_deleted= ...

  10. ssh免密码登录服务器

    A机为本地主机(即用于控制其他主机的机器) B机为远程主机(即被控制的机器server)B机:192.168.3.145 假如A机无密码登录B机器     A机上的命令: 1,ssh-keygen - ...