我正在做一个家庭作业,我们正在创建一个用于程序的类,用于单独或全部计算一次进行基本数学计算。所以加,减,乘,除或全四。
我认为大部分代码都很好,但是在用户输入数字并选择了一个插值方法之后我无法打印它。我试过print(Week7.addNum),print(Week7.addNum()),print(Week7.addnum(numOne,numTwo))。我得到各种错误或没有。随着印刷品(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 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-----
等我也更改了订单,因为它与显示的内容无关。
您需要创建类的实例。尝试类似的东西:
instance = Week7(numOne, numTwo)
if which_Function == 1:
print(instance.addNum())
elif which_Function == 2:
print(instance.subtNum())
...
我命名为instance
的对象将作为self
传递给方法,因为它是你所称的它们。当你查找instance.addNum
时,你会得到一个“绑定方法”对象,它会自动为你传递参数。