Python3 : 未能引发 TypeError
Python3 : Fail to raise TypeError
我想在 class 中的方法参数为非整数时引发 TypeError,但我失败了。代码如下,我在第一个参数中放了一个"N",期望得到一个TypeError,打印出“can't set the Rectangle to a non-integer value”,但是我得到的却是“追溯(最近一次通话最后一次):
文件“/Users/Janet/Documents/module6.py”,第 19 行,位于
r1.setData(N,5)
NameError: 名称 'N' 未定义"
class Rectangle:
def __init__ (self):
self.height = 0
self.width = 0
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
def __str__(self):
return "height = %i, and width = %i" % (self.height, self.width)
r1 = Rectangle()
try:
r1.setData(N,5)
except ValueError:
print ("can't set the Rectangle to a negative number")
except TypeError:
print ("can't set the Rectangle to a non-integer value")
print (r1)
考虑使用 typeof,如这个问题:Checking whether a variable is an integer or not
顺便说一下,您的列表中的格式有问题。改变这个:
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
对此:
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
编辑答案以反映更准确的新解释。
正如@Evert 所说,N 未定义,因此 Python 正在查看变量 N 是什么,但没有找到任何东西。如果您改为编写 "N"(使其成为字符串),那么您的程序应该 return TypeError。
class Rectangle:
def __init__ (self):
self.height = 0
self.width = 0
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
def __str__(self):
return "height = %i, and width = %i" % (self.height, self.width)
r1 = Rectangle()
try:
r1.setData("N",5)
except ValueError:
print ("can't set the Rectangle to a negative number")
except TypeError:
print ("can't set the Rectangle to a non-integer value")
print (r1)
打印出来:
“无法将矩形设置为非整数值
高度 = 0,宽度 = 0"
我想在 class 中的方法参数为非整数时引发 TypeError,但我失败了。代码如下,我在第一个参数中放了一个"N",期望得到一个TypeError,打印出“can't set the Rectangle to a non-integer value”,但是我得到的却是“追溯(最近一次通话最后一次): 文件“/Users/Janet/Documents/module6.py”,第 19 行,位于 r1.setData(N,5) NameError: 名称 'N' 未定义"
class Rectangle:
def __init__ (self):
self.height = 0
self.width = 0
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
def __str__(self):
return "height = %i, and width = %i" % (self.height, self.width)
r1 = Rectangle()
try:
r1.setData(N,5)
except ValueError:
print ("can't set the Rectangle to a negative number")
except TypeError:
print ("can't set the Rectangle to a non-integer value")
print (r1)
考虑使用 typeof,如这个问题:Checking whether a variable is an integer or not
顺便说一下,您的列表中的格式有问题。改变这个:
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
对此:
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
编辑答案以反映更准确的新解释。 正如@Evert 所说,N 未定义,因此 Python 正在查看变量 N 是什么,但没有找到任何东西。如果您改为编写 "N"(使其成为字符串),那么您的程序应该 return TypeError。
class Rectangle:
def __init__ (self):
self.height = 0
self.width = 0
def setData(self, height, width):
if type(height) != int or type(width) != int:
raise TypeError()
if height <0 or width <0:
raise ValueError()
self.height = height
self.width = width
def __str__(self):
return "height = %i, and width = %i" % (self.height, self.width)
r1 = Rectangle()
try:
r1.setData("N",5)
except ValueError:
print ("can't set the Rectangle to a negative number")
except TypeError:
print ("can't set the Rectangle to a non-integer value")
print (r1)
打印出来: “无法将矩形设置为非整数值 高度 = 0,宽度 = 0"