高阶导数
我正在尝试写一个Python程序,让用户输入一个多项式,然后计算它的导数。我的代码是:
print ("Derviatives: ")
k5 = raw.input("Enter 5th degree + coefficent: ")
k4 = raw.input("Enter 4th degree + coefficent: ")
k3 = raw.input("Enter 3rd degree + coefficent: ")
k2 = raw.input("Enter 2nd degree + coefficent: ")
k1 = raw.input("Enter 1st degree + coefficent: ")
k0 = raw.input("Enter constant: ")
int(k5)
int(k4)
int(k3)
int(k2)
int(k1)
int(k0)
print (k5, " ", k4, " ", k3, " ", k2, " ", k1, " ", k0)
1in = raw.input("Correct Y/N?")
if (1in != Y)
k5 = raw.input("Enter 5th degree + coefficent: ")
k4 = raw.input("Enter 4th degree + coefficent: ")
k3 = raw.input("Enter 3rd degree + coefficent: ")
k2 = raw.input("Enter 2nd degree + coefficent: ")
k1 = raw.input("Enter 1st degree + coefficent: ")
k0 = raw.input("Enter constant: ")
else
"""CODE GOES HERE"""
我还是个刚入门的Python程序员,所以对一些基本的语法问题还不是很清楚。有没有什么库是我应该导入的呢?
3 个回答
0
我看到你的代码有以下几个问题:
- 变量名不能以数字开头,比如
1in
。 - 你有一个未定义的变量
Y
。我猜你是想和一个字符串比较,所以应该写成"Y"
。 - 在Python的
if
语句中,括号不是必须的,但结尾的冒号是必须的。else
也是一样。 - 用一个系数列表会比用一堆变量更好。
- 应该是
raw_input
,而不是raw.input
。 - 虽然直接用
int(k5)
可以验证值(如果字符串不是整数就会报错),但这不会改变变量的内容,所以k5
仍然会是一个字符串。
我想展示一点代码,虽然我不是在帮你完成任务(实现算法),而只是展示语言的特性。所以...我会这样做:
# There are no do...while-style loops in Python, so we'll do it this way
# loop forever, and break when confirmation prompt gets "y"
while True:
k = [] # That's an empty list.
# I'm assuming Python 2.x here. Add parentheses for Python 3.x.
print "Derivatives:"
# This range will work like a list [5, 4, 3, 2, 1].
for n in range(5, 1, -1):
k.append(int(raw_input("Enter {0}th degree + coefficient: ".format(n)))
## Or you could do it this way:
#for num in ["5th", "4th", "3rd", "2nd", "1st"]:
# k.append(int(raw_input("Enter {0} degree + coefficient: ".format(num)))
k.append(int(raw_input("Enter constant: ".format(n)))
# Here's a bit tricky part - a list comprehension.
# Read on those, they're useful.
# We need the comprehension, because our list is full of ints,
# and join experts a list of strings.
print " ".join(str(x) for x in k)
## If you didn't get the previous line - that's fine,
## it's fairly advanced subject. Just do like you're used to:
#print k[0], " ", k[1], " ", k[2], " ", k[3], " ", k[4], " ", k[5]
# Oh, and did you notice 5th coeff. is at k[0]?
# That's because we appended them to the list in reverse order.
# Let's reverse the list in-place, so the constant will be k[0] and so on.
k.reverse()
# You don't always need an intermediate variable for single-use values,
# like when asking for confirmation. Just put raw_input call directly
# in if statement condition - it would work just fine.
#
# And let's be case-insensitive here and accept both "Y" and "y"
if raw_input("Correct [Y/N]? ").lower() == "y":
break
1
好的,使用 raw_input
而不是 raw.input
。它是内置的,和 int
一样,所以不需要导入任何东西。当你想把输入转换成整数(int
)时,记得要把结果赋值给一个变量,不然什么都不会改变。你可以把这些功能连在一起使用,比如 k5 = int(raw_input("提示.. "))
。另外,正如 Evert 提到的,变量名不能以数字开头,所以 1in
需要改一下。这个代码应该可以正常工作:
print("Derviatives: ")
k5 = raw_input("Enter 5th degree + coefficent: ")
k4 = raw_input("Enter 4th degree + coefficent: ")
k3 = raw_input("Enter 3rd degree + coefficent: ")
k2 = raw_input("Enter 2nd degree + coefficent: ")
k1 = raw_input("Enter 1st degree + coefficent: ")
k0 = raw_input("Enter constant: ")
k5 = int(k5)
k4 = int(k4)
k3 = int(k3)
k2 = int(k2)
k1 = int(k1)
k0 = int(k0)
print(k5, " ", k4, " ", k3, " ", k2, " ", k1, " ", k0)
in1 = raw_input("Correct Y/N?")
if in1 != "Y":
k5 = raw_input("Enter 5th degree + coefficent: ")
k4 = raw_input("Enter 4th degree + coefficent: ")
k3 = raw_input("Enter 3rd degree + coefficent: ")
k2 = raw_input("Enter 2nd degree + coefficent: ")
k1 = raw_input("Enter 1st degree + coefficent: ")
k0 = raw_input("Enter constant: ")
else:
"""CODE GOES HERE"""
还有,检查一下你使用的 Python 版本。如果是 Python 3,你需要把 raw_input
改成 input
。如果你用的是 Python 2,打印语句就不需要括号。例如,print("Derviatives: ")
应该改成 print "Derviatives: "
。
0
使用Sympy库:
在代码的最上面加上这两行:
from sympy import *
import numpy as np
然后在"""代码在这里"""的地方加上这些:
x = Symbol('x')
y = k5*x**5 + k4*x**4 + k3*x**3 + k2*x**2 + k1*x +constant
yprime = y.diff(x)
yprime