Python 函数中的错误
当我运行代码时,出现以下错误:Python 函数中的错误,python,function,Python,Function,当我运行代码时,出现以下错误: <function setDegreesAndMinutes at 0x10e7cf6e0> 如果有人知道发生了什么,那就太好了 这根本不是一个错误。。。您打印了一个函数对象 您从未调用函数将(self,degrees,minutes)作为参数传递给设置degrees和minutes 另外,通常self意味着您已经在类中定义了该方法,因此您需要该类的实例来调用setdegrees和minutes。在这种情况下,您需要returnValue=some\
<function setDegreesAndMinutes at 0x10e7cf6e0>
如果有人知道发生了什么,那就太好了 这根本不是一个错误。。。您打印了一个函数对象 您从未调用函数将
(self,degrees,minutes)
作为参数传递给设置degrees和minutes
另外,通常self
意味着您已经在类中定义了该方法,因此您需要该类的实例来调用setdegrees和minutes
。在这种情况下,您需要returnValue=some\u class.setdegrees和minutes(degrees,minutes)
那么,您真的需要参数吗?您调用了input
,这意味着您正在提示输入值,因此重新分配您本来会传递到方法中的值。如果确实要提示输入方法,请从(self,degrees,minutes)
中删除度,分钟,然后可以执行返回值=某些类。setdegrees和分钟()
最后,在我看来,setter在Python中并不是真正必要的。此外,它们通常不会自行返回值或提示输入。简单一点-只需设置值
总之,我想你是在找这样的东西
class Foo():
def __init__(self, degrees, minutes):
self.degrees = degrees
self.minutes = minutes
degrees = int(input("degrees: "))
if not isinstance(degrees, int):
raise ValueError("degrees should be int")
minutes = float(input("minutes: "))
if not isinstance(minutes, (int,float)): # I think you had extra parenthesis after minutes
raise ValueError("minutes should be int or float")
f = Foo(degrees, minutes)
print(str(f.degrees) + 'd' + str(f.minutes))
如果需要设置值,则对任何X
值执行f.degrees=X
,例如是否准确复制缩进?您知道缩进在Python中很重要吗?如果是这样,您应该修复缩进,并检查/确认问题仍然存在。我注意到,在许多其他问题中,您实际上还没有调用函数。@jwpfox:请不要尝试修复Python中问题的缩进。另外,您在编辑中删除了一个重要的右括号。谢谢大家。我是一名国际学生,这是我在美国学习的第一个月,所以我花了很长时间才弄明白你们都说了些什么。再次感谢,我仍然在学习,非常高兴你能回答我的问题。我是一名没有英语环境的国际学生,这是我在美国学习的第一个月,所以我花了很长时间才弄明白你们都说了些什么。我仍然在那里发现了一个问题,当我运行你的代码时,它返回“TypeError:setDegreesAndMinutes()正好需要3个参数(给定0)”,但实际上我没有输入任何内容,我没有机会输入它,但得到了这个答案。我能够很好地运行这段代码。为什么在setdegrees和minutes
不是我编写的代码的一部分时会出现错误?打印字符串时仍然存在一些问题。我正在尝试处理它>我不知道你的代码是什么样子,但我提供的运行方式
class Foo():
def __init__(self, degrees, minutes):
self.degrees = degrees
self.minutes = minutes
degrees = int(input("degrees: "))
if not isinstance(degrees, int):
raise ValueError("degrees should be int")
minutes = float(input("minutes: "))
if not isinstance(minutes, (int,float)): # I think you had extra parenthesis after minutes
raise ValueError("minutes should be int or float")
f = Foo(degrees, minutes)
print(str(f.degrees) + 'd' + str(f.minutes))