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

大卫·道

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

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

阿耳emi弥斯

编辑后的代码,应该可以工作:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章