首页 > 编程语言 >python9-eg

python9-eg

时间:2022-10-27 13:23:31浏览次数:48  
标签:__ apple eg python9 苹果 print children result

实例01(模拟幼儿园分苹果)

1 def devition(): 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,"个,剩下",remain,"个。") 10 else: 11 print(apple,"个苹果,平均分给",children,"个小朋友,每人分",result,"个。") 12 if __name__=='__main__': 13 devition()

 

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

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,"个,剩下",remain,"个。") 10 else: 11 print(apple,"个苹果,平均分给",children,"个小朋友,每人分",result,"个。") 12 if __name__=='__main__': 13 try: 14 division() 15 except ZeroDivisionError: 16 print("\n出错了~_~苹果不能被0个小朋友分!")

 

 

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

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

 

 

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

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,"个,剩下",remain,"个。") 11 else: 12 print(apple,"个苹果,平均分给",children,"个小朋友,每人分",result,"个。") 13 #if __name__=='__main__': 14 try: 15 division() #调用分苹果函数 16 except AssertionError as e: 17 print("\n输入有误:",e)

 

标签:__,apple,eg,python9,苹果,print,children,result
From: https://www.cnblogs.com/ron312/p/16831872.html

相关文章

  • keras中的keras.utils.to_categorical方法
    ​​to_categorical(y,num_classes=None,dtype='float32')​​将整型标签转为onehot。y为​​int​​数组,num_classes为标签类别总数,大于max(y)(标签从0开始的)。返回:如果nu......
  • python遇到 Segmentation fault (core dumped) 错误
    问题描述在linux服务器上运行代码遇到这个错误。解决方案重置了python的env环境。由于python包互相依赖,单独升级某个包未能解决问题,遂决定重新create虚拟环境。命令如下:cond......
  • keras SegNet实现
    代码位置​​https://github.com/lsh1994/keras-segmentation​​​模型结构我这里用到了vgg16微调作为编码器,读者可以参照着自定义层对称的编解码结构。训练结果......
  • keras SegNet使用池化索引(pooling indices)
    keras中不能直接使用池化索引。最近学习到SegNet(网上许多错的,没有用池化索引),其中下采样上采样用到此部分。此处用到自定义层。完整测试代码如下。"""@author:LiShiHang@so......
  • preg_replace /e 模式下的代码执行问题
    preg_replace/e模式下的代码执行问题preg_replace在/e模式下存在代码执行问题这里借用例题分析functioncomplex($re,$str){returnpreg_replace(......
  • 编译TDengineGUI 记录
    下载TDengineGUI代码,执行命令:gitclonehttps://github.com/skye0207/TDengineGUI.git安装依赖(直接npminstall失败,参考以下步骤可构建成功)--替换阿里源n......
  • 在不受支持的 Mac 上安装 macOS Ventura、Monterey、Big Sur (OpenCore Legacy Patche
    请访问原文链接:https://sysin.org/blog/install-macos-13-on-unsupported-mac/,查看最新版。原创作品,转载请保留出处。作者主页:www.sysin.orgOpenCoreLegacyPatcher现......
  • centos使用ffmpeg+srs实现笔记本电脑摄像头推流监控
    一、安装ffmpeg1、包管理安装或者二进制编译安装(比较慢)都可以[root@localhost~]#ffmpeg-versionffmpegversion4.1.6Copyright(c)2000-2020theFFmpegdevelope......
  • AtCoder Beginner Contest 266 Ex Snuke Panic (2D)
    AtCoder传送门洛谷传送门考虑dp,\(f_i\)设在\(t_i\)时刻到达第\(i\)个点,能获得的最大收益。转移:\(f_i=f_j+a_i\),其中\(j\)能转移到\(i\)的充要条件有:\(......
  • AtCoder Beginner Contest 201 题解
    vp情况:过了A到E,F没时间也不会。vp总结:ABC表现可以。D慢了一点,写之前大概考虑清楚每个变量或函数的意义,结构明晰才能更快的写出代码。E花的时间太长,原因......