鱼C论坛

 找回密码
 立即注册
查看: 1016|回复: 4

[已解决]求助!

[复制链接]
发表于 2020-8-14 18:04:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想问怎么添加raise进去呀?谢谢!!!

编写一个函数get_volume(radius,height),该函数将圆柱的半径和高度作为参数,并返回圆柱的体积。将结果四舍五入到最接近的整数。函数应验证值的类型,半径的值和高度的值。
该函数应返回“ERROR: Radius must be positive.”  如果半径为负,则返回“ERROR: Height must be positive.”   如果高度为负,则如果两者均为负,则返回“ERROR: Height and radius must be positive”。
当两个参数中的任何一个为0时,该函数都应返回  “ERROR: Not a cylinder” 圆柱体的体积由以下公式给出:math.pi*(r^2)*h

注意:您*必须*在解决方案中使用“ try ... except”语法和“ raise”

这个分别是Test 和 Result
print(get_volume(10, 2))
628
print(get_volume(-10, 2))
ERROR: Radius must be positive.
print(get_volume(10, -2))
ERROR: Height must be positive.
print(get_volume(-10, -2))
ERROR: Height and radius must be positive.
print(get_volume(10, 0))
ERROR: Not a cylinder.
print(get_volume('ten', 2))
ERROR: Invalid input.

  1. import math
  2. def get_volume(radius,height):
  3.     try:
  4.         V=round(math.pi*radius*radius*height);
  5.     except:
  6.         return "ERROR: Invalid input.";
  7.     if(radius<0 and height<0):
  8.         return "ERROR: Height and radius must be positive.";
  9.     elif(radius<0):
  10.         return "ERROR: Radius must be positive.";
  11.     elif(height<0):
  12.         return "ERROR: Height must be positive.";
  13.     elif(radius==0 or height==0):
  14.         return "ERROR: Not a cylinder.";
  15.     return V;
复制代码
最佳答案
2020-8-14 18:14:27
MIQIWEI 发表于 2020-8-14 18:13
但是这样改的话  后面的test就出问题了 ~有点不太对

这样捏?

  1. import math
  2. def get_volume(radius,height):
  3.     try:
  4.         V=round(math.pi*radius*radius*height);
  5.     except:
  6.         return "ERROR: Invalid input.";
  7.     try:
  8.         raise Exception
  9.     except:
  10.         pass
  11.     if(radius<0 and height<0):
  12.         return "ERROR: Height and radius must be positive.";
  13.     elif(radius<0):
  14.         return "ERROR: Radius must be positive.";
  15.     elif(height<0):
  16.         return "ERROR: Height must be positive.";
  17.     elif(radius==0 or height==0):
  18.         return "ERROR: Not a cylinder.";
  19.     return V;
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-14 18:06:34 | 显示全部楼层
  1. import math
  2. def get_volume(radius,height):
  3.     try:
  4.         V=round(math.pi*radius*radius*height);
  5.     except:
  6.         raise TypeError("ERROR: Invalid input.");
  7.     if(radius<0 and height<0):
  8.         raise ValueError("ERROR: Height and radius must be positive.");
  9.     elif(radius<0):
  10.         raise ValueError("ERROR: Radius must be positive.");
  11.     elif(height<0):
  12.         raise ValueError("ERROR: Height must be positive.");
  13.     elif(radius==0 or height==0):
  14.         raise ValueError("ERROR: Not a cylinder.");
  15.     return V;
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-14 18:13:13 | 显示全部楼层

但是这样改的话  后面的test就出问题了 ~有点不太对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-14 18:14:27 | 显示全部楼层    本楼为最佳答案   
MIQIWEI 发表于 2020-8-14 18:13
但是这样改的话  后面的test就出问题了 ~有点不太对

这样捏?

  1. import math
  2. def get_volume(radius,height):
  3.     try:
  4.         V=round(math.pi*radius*radius*height);
  5.     except:
  6.         return "ERROR: Invalid input.";
  7.     try:
  8.         raise Exception
  9.     except:
  10.         pass
  11.     if(radius<0 and height<0):
  12.         return "ERROR: Height and radius must be positive.";
  13.     elif(radius<0):
  14.         return "ERROR: Radius must be positive.";
  15.     elif(height<0):
  16.         return "ERROR: Height must be positive.";
  17.     elif(radius==0 or height==0):
  18.         return "ERROR: Not a cylinder.";
  19.     return V;
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-14 18:17:01 | 显示全部楼层

对啦!谢谢大佬!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 13:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表