Only she needs to calculate the result of this formula for various values of x

Asked

Viewed 89 times

-2

(x**2 - 4*x + 5). Only it needs to calculate the result of this formula for several values of x.

Input format: Two integers, one in each row:

The first number (a) represents the first value of x to be used The second number (b) represents the last value of x to be used

Output format: The results of x**2 - 4*x + 5, for x in the closed interval between a and b in the domain of integers.

Note: the results will always be whole numbers, no rounding is required.

I tried that way, but I didn’t succeed:

x = int(input(''))
x1 = int(input(''))


conta = ((x**2 ) - (4*x) + 5)
conta1 = ((x1**2 ) - (4*x1) + 5)
print(conta)
print(conta1)

1 answer

1

You can create a function where the parameter is x to do the calculations:

def f(x):
    return (x**2) - (4*x) + 5

Here you receive user values:

x1 = int(input('Inicial: '))
x2 = int(input('Final: '))

Here you iterate over the initial and final number range typed by the user, so you call the function for each range value:

for x in range(x1, x2 + 1):
    print(f'resultado da conta: {f(x)}')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.