简体   繁体   中英

How to create a program to calculate a polynomial in python?

So this is the directions to my homework:

Write a program to calculate a polynomial. The program should ask for the user for a,b,c, and x. It should ouput the value of ax^2+ bx +c by calling a function named CalcPoly(a,b,c,x) that returns the value of the function to the main program. The main program should print, not the function.

And this is what I have so far:

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()

And the error I got was:

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'

I don't know how to fix it and I don't even know if I did the code right. I would appreciate any assistance. Thank you so much!

Try this:


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

The function CalcPoly expects the arguments of the polynomial coefficients and the value of variable x .

You can call the function correctly as follows:

CalcPoly(1,2,3,4)

This will evaluate the equation as x^2 + 2x + 3 where x=4 .

But, if you are reading inputs, you must define function as follows:

def CalcPoly()

And also, please do not forget to read input for the value of x .

There are a lot of things wrong in your code.

First, the reason for the error itself-

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

This takes in 4 arguments, a , b , c , and x , you pass none of them when calling the function-

CalcPoly()

Now, that is far from the end of the story. In python you take input like this-

a = int(input())

This gets user input and stores it into a . I'm assuming that's what you wanted to do, any argument within the parens of input() means that will be printed as a prompt-

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

This will prompt the user,

Enter the first degree:

then wait for user input, turn it into an int and store it inside a

All the other input should be the same.

Your question says you should be taking input for a , b , c , and x , not just a , b , c .

So your fixed up function should look like-

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

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

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

 c = int(input("Enter the third degree: "))
 
 x = int(input("Enter the x value: "))

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

This is really basic stuff, you should read the python docs tutorial

Since you say that the main program should print and not the function, you may try this.

def CalcPoly(a, b, c, x):
      result = (a*x**2) + (b*x) + c
      return result
a = int(input("Enter the coefficient of x^2 (a): "))
b = int(input("Enter the coefficient of x (b): "))
c = int(input("Enter the constant (c): "))
x = int(input("Enter x :"))
value = CalcPoly(a, b, c,x)
print(value)

Also you were facing an issue with your code, that is -

TypeError: CalcPoly() missing 4 required positional arguments: 'a', 'b', 'c', and 'x'

This is because while defining the CalcPoly() function you have declared 4 positional arguments a, b, c and x and while calling the function you haven't given the values of a, b, c and x in the function call.

I'd like to add an object-oriented approach here. Let's create a class where the method get_input collects inputs from users and the method __call__ basically performs the polynomial calculation. This way you can separate the process of data collection and the actual calculation.

from typing import Union

class CalcPoly:
    def get_input(self):
        a: int = int(input("Enter the first degree:"))
        b: int = int(input("Enter the second degree:"))
        c: int = int(input("Enter the third degree:"))
        x: Union[int, float] = input("Enter x:")
        return a, b, c, x

    def __call__(self):
        a, b, c, x = self.get_input()
        return (a*x**2) + (b*x) + c

calc = CalcPoly()
print(calc())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM