猿问

求解单项和多项式 Python 类

我是第一次尝试类,我想创建一个程序,要求用户输入 a、b 和 c,然后为打印语句中所述的方程形式求解 x。但是,我的类有问题,给我一个错误,我没有使用类中的变量,缺少 5 个位置参数。任何帮助都会很棒,非常感谢。


class EquationSolver:

    def MonomialSolver(self,a,b,c,x):

        a = input("Enter Input for a:")

        b = input("Enter Input for b:")

        c = input("Enter input for c:")

        x = (c+b)/a

        print("For the equation in the format ax-b=c, with your values chosen x must equal", x)

    def PolynomialSolver(self,a,b,c,x):

        a = input("Enter Input for a:")

        b = input("Enter Input for b:")

        c = input("Enter input for c:")

        x = (c^2 + b) / a

        print("For the equation in the format sqrt(ax+b) = c, with your values chosen x must equal", x)

    MonomialSolver()

    PolynomialSolver()


慕森王
浏览 91回答 1
1回答

慕运维8079593

我看到的问题是函数的输入。您不需要 self 参数或任何其他参数。这些函数应该在循环之外运行。编辑后的版本应该像这样循环:class EquationSolver:    def MonomialSolver():        # Uses float() to turn input to a number        a = float(input("Enter Input for a:"))        b = float(input("Enter Input for b:"))        c = float(input("Enter input for c:"))        x = (c+b)/a        print("For the equation in the format ax-b=c, with your values chosen x must equal", x)    def PolynomialSolver():        a = float(input("Enter Input for a:"))        b = float(input("Enter Input for b:"))        c = float(input("Enter input for c:"))        x = (c^2 + b) / a        print("For the equation in the format sqrt(ax+b) = c, with your values chosen x must equal", x)EquationSolver.MonomialSolver()EquationSolver.PolynomialSolver()
随时随地看视频慕课网APP

相关分类

Python
我要回答