Let's solve your problem, step by step.
First, let's see the use of range
. Let's look at the documentation and ... just for life, it only accepts whole numbers! This function accepts up to 3 parameters (check this answer for more chewed details). How do we proceed then?
An alternative is to proceed arithmetically. If we want to get the values between m
and M
jumping from 0.5
to 0.5
(being i
iterator), this is identical to getting values between 2*m
and 2*M
jumping from 1
in 1
, iterating in variable i_dobro
that can easily be transformed into i = i_dobro/2
. But this only works in Python 3 (for Python 2 you need to split by 2.0
) and if m
and M
are integers ...
So maybe we can use while
? Let's start from m
, while less than M
, increasing 0.5
at each step ... yeah, seems reasonable.
Now, how about finding out who is m
and M
that I quoted above? Well, m
is the bottom margin of the range, while M
is the top margin. As these data are read from the standard input, and have no restriction or supplementary text to this problem, I can only believe that the two inputs will be possibly out of order numbers. Then we read n1
for the first number and n2
for the second number. To determine m
and M
is as follows:
(M, m) = (n1, n2) if n1 > n2 else (n2, n1)
Okay, so overall our iteration looks like this (I'll keep reading it as much as I disagree with it):
n1 = eval(input())
n2 = eval(input())
(M, m) = (n1, n2) if n1 > n2 else (n2, n1)
i = m
while i < M:
# ação interessante da iteração
i += 0.5
So how do you do the interesting iteration action? The desired result is the impression of the evaluation of the polynomial against the% of% past. I see the following options:
- calculate directly on
i
- define a function a priori and call it cheerfully
- create a lambda and call it even happier
For the first option, it is only necessary to change the annotated section:
print(3* i**3 -5*i + 0.8)
For the second option, before doing the readings due, define the function like this:
def polinomio(x):
return 3* x**3 -5*x + 0.8
Then call within print
:
print(polinomio(i))
For the lambda-alternative, before the iteration, create the lambda function and assign it to the variable print
:
polinomio = (lambda x: 3* x**3 -5*x + 0.8)
And call within polinomio
as if it were a traditional function:
print(polinomio(i))