首页 > 解决方案 > 如何在python中创建一个计算多项式的程序?

问题描述

所以这是我作业的方向:

编写程序计算多项式。程序应该询问用户 a、b、c 和 x。它应该通过调用一个名为 CalcPoly(a,b,c,x) 的函数来输出 ax^2+ bx +c 的值,该函数将函数的值返回给主程序。主程序应该打印,而不是函数。

这就是我到目前为止所拥有的:

def CalcPoly(a, b, c, x):

 print ("Enter the first degree: ")
 print (int(input(a)))

 print ("Enter the second degree: ")
 print (int(input(b)))

 print ("Enter the third degree: ")
 print (int(input(c)))

 print (a*x**2 + b*x + c)

CalcPoly()

我得到的错误是:

Traceback (most recent call last):
 File "main.py", line 14, in <module>
  CalcPoly()
TypeError: CalcPoly() missing 4 required positional arguments: 'a', 'b', 'c', and 'x'

我不知道如何修复它,我什至不知道我的代码是否正确。我将不胜感激任何帮助。非常感谢!

标签: python

解决方案


试试这个:


def CalcPoly(a, b, c, x):
    print (a*x**2 + b*x + c)

if __name__=="__main__":
    a = int(input("Enter the first degree: "))
    b = int(input("Enter the second degree: "))
    c = int(input("Enter the third degree: "))
    
    CalcPoly(a, b, c, 4) # x=4

    # Console:
    # Enter the first degree: 1
    # Enter the second degree: 2
    # Enter the third degree: 3
    # 27


推荐阅读