这是我第一次尝试创建和使用类。当我要求用户输入时发生错误。我收到以下错误:
n1 = Arithmetic.float_input("Enter your First number: ")
TypeError: float_input() missing 1 required positional argument: 'msg'
这是我的代码。
# Define class
class Arithmetic:
def float_input(self, msg): # Use this function for exception handling during user input
while True:
try:
return float(input(msg))
except ValueError:
print("You must enter a number!")
else:
break
def add(self, n1, n2):
sum1 = n1 + n2
print(n1,"+" ,n2,"=", sum1)
def sub(self, n1, n2):
diff = n1 - n2
print(n1,"-",n2,"-", diff)
def mult(self, n1, n2):
product = n1 * n2
print(n1,"*",n2, "=", product)
def div(self, n1, n2):
if n2 == 0:
print(n1, "/",n2,"= You cannot divide by Zero")
else:
quotient = n1 / n2
print(n1, "/",n2,"=", quotient)
def allInOne(self, n1, n2):
#Store values in dictionary (not required, just excercising dictionary skill)
res = {"add": add(n1, n2), "sub": sub(n1, n2), "mult": mult(n1, n2), "div": div(n1, n2)}
# Declare variables. Ask user for input and use the exception handling function
n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")
我想念什么?
答案 0 :(得分:5)
如果您来自Java背景,那么值得一提的是,除非您需要self
提供的状态,否则通常不需要在Python中的类中包装方法。
无论如何,您看到的错误是因为您的方法未标记为@classmethod
或@staticmethod
,因此需要类的实例,而您只是通过类本身来调用它们(因此不会将隐式实例或类对象作为第一个参数传入。
因此,您的选择是:
1 –创建Arithmetic()
的实例并使用它:
arith = Arithmetic()
n1 = arith.float_input("Enter your First number: ")
n2 = arith.float_input("Enter your Second number: ")
2 –将方法标记为静态,例如
@staticmethod
def float_input(prompt): # note: no `self`
3 –标记方法类方法,例如
@classmethod
def float_input(cls, prompt): # `cls` is `Arithmetic` (or its subclass) itself
4 –使方法成为没有类的常规函数。
答案 1 :(得分:2)
问题是您在调用方法之前没有创建Arithmetic
的实例。因为您没有创建对象,所以不会将任何实例传递给self
参数。这将导致消息"Enter your first number:"
传递给 self 参数,而msg
参数为空。
要解决此问题,只需在类名称后使用括号创建一个对象,例如:
# Declare variables. Ask user for input and use the exception handling function
n1 = Arithmetic().float_input("Enter your First number: ")
n2 = Arithmetic().float_input("Enter your Second number: ")
如果您不是故意创建对象,则可以使用@classmethod
类修饰符将类名传递给 self 参数。
# Define class
class Arithmetic:
@classmethod
def float_input(class_, msg): # Use this function for exception handling during user input
while True:
try:
return float(input(msg))
except ValueError:
print("You must enter a number!")
else:
break
# Code...
n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")
还有另一个名为@staticmethod
的装饰器。如果使用此装饰器,则可以在不具有算术实例且无需在方法签名中定义 self 的情况下调用该方法。示例:
class Arithmetic:
@staticmethod
def float_input(msg): # Use this function for exception handling during user input
while True:
try:
return float(input(msg))
except ValueError:
print("You must enter a number!")
else:
break
# Code...
n1 = Arithmetic.float_input("Enter your First number: ")
n2 = Arithmetic.float_input("Enter your Second number: ")
答案 2 :(得分:0)
修复为:
# Declare variables. Ask user for input and use the exception handling function
arithmatic = Arithmetic()
n1 = arithmatic.float_input("Enter your First number: ")
n2 = arithmatic.float_input("Enter your Second number: ")
答案 3 :(得分:0)
最好先实例化该类,然后使用它的正确方法,如下所示
n1 = new Arithmetic()
n1.float_input('Enter your First number: ')