r/pythonhelp • • Sep 06 '22

Function with multiple x

Hey I'm totally new to programming and I struggle with this task, so if anyone could help me out that would mean a lot.

Write a program that can calculate values of f (x) for the following function:

f(x)=x^-0.2, 0 < x <= 1, when x = (-0.4, 0.0, 0.4, 0.8, 1.2)

I tried this: but got the error "ZeroDivisionError: 0.0 cannot be raised to a negative power":

values=[-0.4, 0.0, 0.4, 0.8, 1.2]

for x in values:

y = x**(-2)

if(x > 0 and x < 1):

print(y)

0 Upvotes

3 comments sorted by

1

u/AmongstYou666 Sep 06 '22

def f(x):
    answer = []
    for i in x:
        if i > 0 and i < 1:
            answer.append(i**-0.2)
        else:
            answer.append(None)
    return(answer)
print(f([-0.4, 0.0, 0.4, 0.8, 1.2]))

1

u/Erdii7 Sep 07 '22

I kept trying this, but it kept getting [None, None, 1.2011244339814313, 1.0456395525912732, None]. It doesn't get all values x. Thank you for your reply tho!

1

u/AmongstYou666 Sep 07 '22

You can see why right? Any value less than 0 and greater than 1 is invalid according to your own function.