首页 > 编程语言 >Python第八周

Python第八周

时间:2022-11-01 19:45:01浏览次数:42  
标签:__ apple Python 第八 苹果 print children result

一.    实验目的和要求

了解Pyhon中常见的异常

二.   实验环境

 python 3.10 64-bit

三.   实验过程

实例1

代码如下:

 1 def division():
 2     '''功能:分苹果'''
 3     print("\n==============分苹果了================\n")
 4     apple = int(input("请输入苹果的个数:"))
 5     children = int(input("请输入来了几个小朋友:"))
 6     result = apple // children
 7     remain = apple - result*children    #计算余下苹果
 8     if remain > 0:
 9         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
10     else:
11         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
12 
13 if __name__ == '__main__':
14     division()

运行结果:

 

实例2

代码如下:

 1 def division():
 2     '''功能:分苹果'''
 3     print("\n==============分苹果了================\n")
 4     apple = int(input("请输入苹果的个数:"))
 5     children = int(input("请输入来了几个小朋友:"))
 6     result = apple // children
 7     remain = apple - result*children    #计算余下苹果
 8     if remain > 0:
 9         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
10     else:
11         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
12 
13 if __name__ == '__main__':
14     try:
15         division()
16     except ZeroDivisionError:
17         print("\n出错了~_~ —— 苹果不能被0个小朋友分!")

 

运行结果:

 

 

实例3

代码如下:

 1 def division():
 2     '''功能:分苹果'''
 3     print("\n==============分苹果了================\n")
 4     apple = int(input("请输入苹果的个数:"))
 5     children = int(input("请输入来了几个小朋友:"))
 6     if apple < children:
 7         raise ValueError("苹果太少了,不够分...")    #抛出异常(reason)
 8     result = apple // children
 9     remain = apple - result*children    #计算余下苹果
10     if remain > 0:
11         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
12     else:
13         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
14 
15 if __name__ == '__main__':
16     try:
17         division()
18     except ZeroDivisionError:
19         print("\n出错了~_~ —— 苹果不能被0个小朋友分!")
20     except ValueError as e:
21         print("\n出错了 ~_~ ——", e)

 

运行结果:

 

 

 

实例4

代码如下:

 1 def division():
 2     '''功能:分苹果'''
 3     print("\n==============分苹果了================\n")
 4     apple = int(input("请输入苹果的个数:"))
 5     children = int(input("请输入来了几个小朋友:"))
 6     assert apple >= children, "苹果不够分"   #应用断言调试
 7     result = apple // children
 8     remain = apple - result*children    #计算余下苹果
 9     if remain > 0:
10         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
11     else:
12         print(apple, "个苹果,平均分给", children, "个小朋友,每人分", result, "个。")
13 
14 if __name__ == '__main__':
15     division()

 

运行结果:

 

标签:__,apple,Python,第八,苹果,print,children,result
From: https://www.cnblogs.com/yisheng8/p/16848906.html

相关文章

  • Python第七周
    一.   实验目的和要求掌握Python中如何定义类、使用类等等二.  实验环境 python3.1064-bit三.  实验过程实例1代码如下:1classGeese:2'''大雁......
  • Python—循环遍历
    一、循环遍历遍历某个结构形成的循环运行方式for<循环变量>in<遍历结构>:<语句块>从遍历结构中逐一提取元素,放在循环变量中:由保留字for和in组成,完整遍历所......
  • Python 4 程序组织结构(顺序执行、条件、循环)
    程序组织结构(顺序执行、条件、循环)python也是顺序结构的哟布尔值可以用bool函数判断是TRUEorFLASE分支结构if:写法怪怪的注意看:image.pngname=''name......
  • python: easyocr的安装和使用(easyocr 1.6.2 / Python 3.7.15 )
    一,安装easyocr:1,官网:https://www.jaided.ai/项目代码地址:https://github.com/JaidedAI/EasyOCR通过pip安装:[root@blog~]#pip3installeasyocr查看......
  • python批量发送post请求
    importrequestsimportjsonimportpandasaspdlist2=[]false=Falsetrue=True#postheaders={"content-type":"charset=utf8"}url="url链接"#获取......
  • python名称空间及作用域
    名称空间存放名字的地方,是对栈区的划分,有了名称空间,可以在栈区中存放相同的名字内置名称空间存放python解释器内置的名称存活周期:python解释器启动-关闭>>>print<......
  • Python项目开发必备神器——virtualenv使用
    Python项目开发必备神器——virtualenv使用安装virtualenv安装virtualenv比较简单,无论是在linux系统上还是windows上都可以通过pip来进行安装。代码如下: pipinstal......
  • python-绘图进阶
    数据准备importmatplotlib.pyplotaspltimporttushareastsimportpandasaspdimportdatetime%matplotlibinlineplt.rcParams['font.sans-serif']=['Arial......
  • Windows下Git Bash运行python的三种办法
    以前运行cmd命令都是在cmd里面的,但是那个页面实在是太丑了,后面我就全部用gitbash来运行window下的命令了。但是在gitbash中运行下python–-version或piplist命令......
  • Python发送QQ邮件
    Python发送QQ邮件1、登陆QQ邮箱,获取授权码可以参考官网说明登录QQ邮箱点击设置点击账户、点击开启POP3/SMEP服务点击开启后验证密保,然后根据操作发送短信......