python - 如何在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'
我不知道如何修复它,我什至不知道我的代码是否正确。我将不胜感激任何帮助。非常感谢!
解决方案
试试这个:
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
推荐阅读
- c++ - 为参数包中的每种类型推导模板参数类型
- r - 如何使用包含“<”-operator 的条目执行计算?在这种情况下,将上限除以二
- angular - 添加图像
- fullcalendar - 加载时不显示完整日历的正文
- javascript - if 语句中的 Snackbar 错误消息
- api - Google SDK 中的项目错误
- mysql - 如何将 Spark SQL 与 My SQL 数据库 Scala 连接起来
- javascript - 在选项卡上更改更新本机反应中的状态
- python - 如何在具有键和键变量的python字典中创建一个列表?
- angular - 如何使用 Let's Encrypt 证书解决“错误:06065064:数字信封例程:EVP_DecryptFinal_ex:错误解密”ij Angular 7?