在类中的单个变量中声明多个值的Python方式

huangapple go评论75阅读模式
英文:

Python declaring multiple values in a single variable in a class

问题

isSolvable()函数应该在计算结果为0时返回False

英文:

I'm trying to call the values that I have inputted into the function solvable. It's not returning a boolean value in response to the input. This is my code:

class CramersRule:
   a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3 = eval(input("Please enter values a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3 of a simultaneous eq: "))
   def __init__(self):
      self.__a1 = a1
      self.__a2 = a2
      self.__a3 = a3
      self.__b1 = b1
      self.__b2 = b2
      self.__b3 = b3
      self.__c1 = c1
      self.__c2 = c2
      self.__c3 = c3
      self.__d1 = d1
      self.__d2 = d2
      self.__d3 = d3   

    def isSolvable(self, a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3):
        if (a1*b2*c3 - a1*b3*c2 -a2*b1*c3 + a2*b3*c1 + a3*b1*c2 - a3*b2*c1) != 0:
            return True
        else:
            return False

The isSolvable() function is supposed to return False if the method calculates the value to be 0.

答案1

得分: 0

欢迎来到Stack Overflow!

尝试这个。

class CramersRule:
    def __init__(self):
        a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3 = eval(input("请输入方程组的系数 a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3: "))
        self.__a1 = a1
        self.__a2 = a2
        self.__a3 = a3
        self.__b1 = b1
        self.__b2 = b2
        self.__b3 = b3
        self.__c1 = c1
        self.__c2 = c2
        self.__c3 = c3
        self.__d1 = d1
        self.__d2 = d2
        self.__d3 = d3

    def isSolvable(self):
        if (self.__a1*self.__b2*self.__c2 - self.__a1*self.__b3*self.__c2 -self.__a2*self.__b1*self.__c3 + self.__a2*self.__b3*self.__c1 + self.__a3*self.__b1*self.__c2 - self.__a3*self.__b2*self.__c1) != 0:
            return True
        else:
            return False

我不确定你在哪里或者如何运行你的函数,但是我能够将代码保存到一个名为 fq.py 的文件中,并在Python REPL中进行验证。

>python
>>>import fq

接下来,创建一个CramersRule类的实例并将其赋给变量f。在提示符中输入数字。

>>>f = fq.CramersRule()

最后,根据你输入的数字看它是True还是False。

>>>f.isSolvable()
英文:

welcome to Stack Overflow!

Try this.

class CramersRule:
    def __init__(self):
        a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3 = eval(input("Please enter values a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3 of a simultaneous eq: "))
        self.__a1 = a1
        self.__a2 = a2
        self.__a3 = a3
        self.__b1 = b1
        self.__b2 = b2
        self.__b3 = b3
        self.__c1 = c1
        self.__c2 = c2
        self.__c3 = c3
        self.__d1 = d1
        self.__d2 = d2
        self.__d3 = d3

    def isSolvable(self):
        if (self.__a1*self.__b2*self.__c2 - self.__a1*self.__b3*self.__c2 -self.__a2*self.__b1*self.__c3 + self.__a2*self.__b3*self.__c1 + self.__a3*self.__b1*self.__c2 - self.__a3*self.__b2*self.__c1) != 0:
            return True
        else:
            return False

I'm not sure how or where you're running your function but I was able to save the code to a file name fq.py and this is how I verified it in the python repl.

>python
>>>import fq

Next create an instance of the CramersRule class and assign it to a variable f. Also input the numbers at the prompt.

>>>f = fq.CramersRule()

Finally, see if it's true or false based on the numbers you input.

>>>f.isSolvable()

huangapple
  • 本文由 发表于 2023年2月10日 05:12:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定