我有一个练习题,需要帮助解决

时间:2019-10-31 04:27:34

标签: python

编写一个名为Polynomial.py的程序,该程序可让用户输入多项式的系数。该程序应:

display the polynomial in its usual form.
(For example, if the user enters a coefficient of 4.6 for x cubed, -7.3 for x squared, 0 for x, and 9.8 for the constant, it would display:  "4.6 x^3 - 7.3 x^2 + 9.8"
Ask the user to enter a value for x, and then display the result when that value is plugged in to the polynomial function.
Allow the user to continue entering values for x until he/she decides to quit.

对于要被视为完整的程序,插入值的计算应该正确。

我还没有从python学到很多东西,这就是为什么我要寻求帮助

poly = int(input("Enter your coefficients here"))
print(poly)

到目前为止,这基本上是我所学的,只是基本的输入和输出。我不确定为什么我必须解决手头的这项任务,但会得到一些帮助。

1 个答案:

答案 0 :(得分:0)

在课程开始时很难给学生这种类型的作业,而讲座仅教您输入和输出命令。

既然是考试期,希望下面的代码对您有所帮助,

poly = int(input("Enter degree of polynomial:"))
print(poly)
coefficients = list()
current_polynomial = ""
order = poly
while order != 0:
    coefficient = int(input("Enter coefficient for x^{0}:".format(order)))
    if coefficient != 0:
        if order < poly and order != 0:
            current_polynomial = current_polynomial + str('+ {0}x^{1}'.format(coefficient, order))
        else:
            current_polynomial = current_polynomial + str(' {0}x^{1}'.format(coefficient, order))
    print(current_polynomial)

    if order != 0 and coefficient == 0:
        order += 1

    order -= 1