高阶导数

时间:2014-09-03 12:09:59

标签: python derivative

我正在尝试编写一个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 :(得分:1)

好的,请使用raw_input代替raw.input。它内置,int,因此无需导入。转换为整数(int)时,需要分配结果,否则不会发生任何变化。您可以链接这些函数,并使用k5 = int(raw_input("prompt.. "))。此外,正如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语句中的括号。例如。 print("Derviatives: ") => print "Derviatives: "

答案 1 :(得分: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

答案 2 :(得分:0)

我发现您的代码存在以下问题:

  1. 变量名称不能以数字开头:1in
  2. 您有未定义的变量Y。我想你想要与字符串文字进行比较,所以请改为写"Y"
  3. python的if运算符中不需要括号,但需要使用尾随冒号。同样适用于else
  4. 系数列表肯定是比一堆变量更好的方法。
  5. 它是raw_input,而不是raw.input
  6. 虽然只是执行int(k5)会验证值(如果字符串不代表整数,则引发异常)它不会影响变量,因此k5仍然会包含一个字符串
  7. 猜测显示一些代码不会有什么坏处,因为我没有在这里执行任务(实现算法),而只是显示语言功能。所以......我会这样做:

    # 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