Hello,
I am a very entry level python user so my knowledge is behind at the moment but I am have a problem creating a function that will take an x range and pass it through as a parameter to my function and output all the results.
For example this is what I have:

def mathFunction():
                fx=(x-1)*(x-3)*(x-5)*(x-7)
                print fx

for x in range(0-8):
                print x, mathFunction(x)

Any help is greatly appreciated.

Thanks!

I think you're looking for something like this:

def mathFunction(x):
    return (x-1)*(x-3)*(x-5)*(x-7)

for x in range(0,8):
    print x, mathFunction(x)

Ranges take in up to 3 arguments, start, stop, and step:
i.e.
range(3) returns 0,1,2
range(0,10) returns 0,1,2,3,4,5,6,7,8,9
range(0,10,2) returns 0,2,4,6,8

There were two things problematic with your math function:
First, it didn't realize it needed a paramater x, and second it didn't return anything for the print function on your original line 6 to output.

Hope it helps,
- Joe

I do have a second question though. The second part is asking for a function that will have a for loop to control the range inclusively. I am assuming this means having the for loop nested within the function, however in doing so the parameter x isnt being called properly. My only guess is that print mathFunction2(x) cannot call the function because the x does not have a value yet?

def mathFunction2(x):
        for x in range(0-8):
                return (x-1)*(x-3)*(x-5)*(x-7)
    
print mathFunction2(x)

any ideas?

For the second part you don't need to call mathFunction2 with a paramater, because the only place you use and create x is within the mathFunction2 body itself. The second thing is that if you want your range to be 0 through 8 inclusive, meaning you want all xs [1,2,3,4,5,6,7,8] you'll need to do a range of range(0,9); doing a range of (0-8) means python is evaluating it as a range of -8, and you can't do a range on negative numbers. If you were to do a range of (0,8) then you'd get the numbers 1-7. The third thing is that now your for loop is in the function, it will only return the first number; so you may want to replace that with a print rather than a return in order to get all outputs rather than just the first:

def mathFunction2():
    for x in range(0,9):
    print (x-1)*(x-3)*(x-5)*(x-7)
     
mathFunction2()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.