在课堂上打电话和打印时遇到问题

时间:2019-03-24 23:29:01

标签: python-3.x class calling-convention

我正在做一项家庭作业,在该作业中,我们将创建一个类,该类将用于程序中,该程序可用于单独进行或一次进行所有基本数学计算。因此,加,减,乘,除或全部四个。

我认为大多数代码都不错,但是在用户输入了数字并选择了算术方法后,我无法将其打印出来。我已经尝试过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”为例。我知道我需要在打印件中加上“ +”和“ =”,但是一旦弄清楚就可以弄清楚在哪里打印。 提前致谢。 戴夫

2 个答案:

答案 0 :(得分:1)

编辑后的代码应该可以正常工作

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-----

我也更改了顺序,因为它与显示的内容无关。

答案 1 :(得分:0)

您需要创建您的类的实例。尝试类似的东西:

instance = Week7(numOne, numTwo)

if which_Function == 1:
   print(instance.addNum())

elif which_Function == 2:
   print(instance.subtNum())

...

我命名为instance的对象将作为self传递到方法中,因为这就是调用它们的对象。当您查找instance.addNum时,会得到一个“绑定方法”对象,该对象会自动为您传递参数。