为了账号安全,请及时绑定邮箱和手机立即绑定

求解单项和多项式 Python 类

求解单项和多项式 Python 类

慕森王 2022-05-24 10:55:04
我是第一次尝试类,我想创建一个程序,要求用户输入 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()
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

我看到的问题是函数的输入。您不需要 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()


查看完整回答
反对 回复 2022-05-24
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号