调用和打印 class 时出现问题
Having a problem with calling and printing out of a class
我正在做一个家庭作业,其中我们正在创建一个 class 用于程序中,用于单独或一次进行所有计算的基本数学计算。所以加、减、乘、除或全部四项。
我认为大部分代码都不错,但在用户输入数字并选择计算方法后,我无法将其打印出来。我试过 print(Week7.addNum)、print(Week7.addNum())、print(Week7.addnum(numOne, numTwo))。我收到各种错误或什么都没有。随着 print(Week7.addnum) 我得到了。我一直在研究添加功能,并想知道我是否可以让它工作,其余的也可以效仿。
class Week7:
def __init__(self, numOne, numTwo):
self.numOne = numOne
self.numTwo = numTwo
def addNum(self):
return (self.numOne + self.numTwo)
def subtNum(self):
return (self.numOne - self.numTwo)
def multNum(self):
return (self.numOne * self.numTwo)
def divNum(self):
if self.numTwo !=0:
return (self.numOne / self.numTwo)
else:
return print('You can not divde by 0')
def allNum(self):
return (self.numOne + self.numTwo, self.numOne - self.numTwo, self.numOne * self.numTwo, self.numOne / self.numTwo )
numOne=int(input("Enter first number: "))
numTwo=int(input("Enter second number: "))
functions = [ "1) Add two numbers",
"2) Mult two numbers",
"3) Divide two numbers",
"4) Subtract two numbers",
"5) All in one: Perform all math Operations",
"6) End Program"
]
for x in functions:
print( x )
print()
which_Function = int(input("Please select what operation you would like to perform: ") )
if which_Function == 1:
print(Week7.addNum)
elif which_Function == 2:
Week7.subtNum(self)
elif which_Function == 3:
Week7.multNum(self)
elif which_Function == 4:
Week7.divNum(self)
elif which_Function == 5:
Week7.allNum(self)
elif which_Function == 6:
exit
我认为除了问题的实际打印外,一切正常。我想以“1 + 2 = 3”为例。我知道我需要在打印输出中放置“+”和“=”,但我完全可以弄清楚在哪里打印。
提前致谢。
戴夫
您需要创建 class 的实例。尝试类似的东西:
instance = Week7(numOne, numTwo)
if which_Function == 1:
print(instance.addNum())
elif which_Function == 2:
print(instance.subtNum())
...
我命名为 instance
的对象将作为 self
传递给方法,因为这是您调用它们的对象。当您查找 instance.addNum
时,您会得到一个 "bound method" 对象,它会自动为您传递参数。
编辑后的代码,应该可以工作:
class Week7:
def __init__(self, numOne, numTwo):
self.numOne = numOne
self.numTwo = numTwo
def addNum(self):
return (self.numOne + self.numTwo)
def subtNum(self):
return (self.numOne - self.numTwo)
def multNum(self):
return (self.numOne * self.numTwo)
def divNum(self):
if self.numTwo !=0:
return (self.numOne / self.numTwo)
else:
return print('You can not divde by 0')
def allNum(self):
return (self.numOne + self.numTwo, self.numOne - self.numTwo, self.numOne * self.numTwo, self.numOne / self.numTwo )
numOne=int(input("Enter first number: "))
numTwo=int(input("Enter second number: "))
w7 = Week7(numOne, numTwo)
functions = [ "1) Add two numbers",
"2) Mult two numbers",
"3) Divide two numbers",
"4) Subtract two numbers",
"5) All in one: Perform all math Operations",
"6) End Program"
]
for x in functions:
print( x )
print()
which_Function = int(input("Please select what operation you would like to perform: ") )
if which_Function == 1:
print(w7.addNum())
elif which_Function == 2:
print(w7.multNum())
elif which_Function == 3:
print(w7.divNum())
elif which_Function == 4:
print(w7.subtNum())
elif which_Function == 5:
print(w7.allNum())
elif which_Function == 6:
exit()
更改说明:
w7 = Week7(numOne, numTwo)
创建 Week7
对象的实例
print(w7.addNum())
调用函数并打印输出。
--ditto--mult-----
等
我也更改了顺序,因为它与显示的内容不相关。
我正在做一个家庭作业,其中我们正在创建一个 class 用于程序中,用于单独或一次进行所有计算的基本数学计算。所以加、减、乘、除或全部四项。
我认为大部分代码都不错,但在用户输入数字并选择计算方法后,我无法将其打印出来。我试过 print(Week7.addNum)、print(Week7.addNum())、print(Week7.addnum(numOne, numTwo))。我收到各种错误或什么都没有。随着 print(Week7.addnum) 我得到了。我一直在研究添加功能,并想知道我是否可以让它工作,其余的也可以效仿。
class Week7:
def __init__(self, numOne, numTwo):
self.numOne = numOne
self.numTwo = numTwo
def addNum(self):
return (self.numOne + self.numTwo)
def subtNum(self):
return (self.numOne - self.numTwo)
def multNum(self):
return (self.numOne * self.numTwo)
def divNum(self):
if self.numTwo !=0:
return (self.numOne / self.numTwo)
else:
return print('You can not divde by 0')
def allNum(self):
return (self.numOne + self.numTwo, self.numOne - self.numTwo, self.numOne * self.numTwo, self.numOne / self.numTwo )
numOne=int(input("Enter first number: "))
numTwo=int(input("Enter second number: "))
functions = [ "1) Add two numbers",
"2) Mult two numbers",
"3) Divide two numbers",
"4) Subtract two numbers",
"5) All in one: Perform all math Operations",
"6) End Program"
]
for x in functions:
print( x )
print()
which_Function = int(input("Please select what operation you would like to perform: ") )
if which_Function == 1:
print(Week7.addNum)
elif which_Function == 2:
Week7.subtNum(self)
elif which_Function == 3:
Week7.multNum(self)
elif which_Function == 4:
Week7.divNum(self)
elif which_Function == 5:
Week7.allNum(self)
elif which_Function == 6:
exit
我认为除了问题的实际打印外,一切正常。我想以“1 + 2 = 3”为例。我知道我需要在打印输出中放置“+”和“=”,但我完全可以弄清楚在哪里打印。 提前致谢。 戴夫
您需要创建 class 的实例。尝试类似的东西:
instance = Week7(numOne, numTwo)
if which_Function == 1:
print(instance.addNum())
elif which_Function == 2:
print(instance.subtNum())
...
我命名为 instance
的对象将作为 self
传递给方法,因为这是您调用它们的对象。当您查找 instance.addNum
时,您会得到一个 "bound method" 对象,它会自动为您传递参数。
编辑后的代码,应该可以工作:
class Week7:
def __init__(self, numOne, numTwo):
self.numOne = numOne
self.numTwo = numTwo
def addNum(self):
return (self.numOne + self.numTwo)
def subtNum(self):
return (self.numOne - self.numTwo)
def multNum(self):
return (self.numOne * self.numTwo)
def divNum(self):
if self.numTwo !=0:
return (self.numOne / self.numTwo)
else:
return print('You can not divde by 0')
def allNum(self):
return (self.numOne + self.numTwo, self.numOne - self.numTwo, self.numOne * self.numTwo, self.numOne / self.numTwo )
numOne=int(input("Enter first number: "))
numTwo=int(input("Enter second number: "))
w7 = Week7(numOne, numTwo)
functions = [ "1) Add two numbers",
"2) Mult two numbers",
"3) Divide two numbers",
"4) Subtract two numbers",
"5) All in one: Perform all math Operations",
"6) End Program"
]
for x in functions:
print( x )
print()
which_Function = int(input("Please select what operation you would like to perform: ") )
if which_Function == 1:
print(w7.addNum())
elif which_Function == 2:
print(w7.multNum())
elif which_Function == 3:
print(w7.divNum())
elif which_Function == 4:
print(w7.subtNum())
elif which_Function == 5:
print(w7.allNum())
elif which_Function == 6:
exit()
更改说明:
w7 = Week7(numOne, numTwo)
创建Week7
对象的实例print(w7.addNum())
调用函数并打印输出。--ditto--mult-----
等
我也更改了顺序,因为它与显示的内容不相关。