user71812
user71812

Reputation: 457

Python function only outputs one result instead of taking different inputs

I am trying to build a Python function that accepts numerous inputs (indefinite) in a list like this:

def limit(x_es):
    for x in x_es:
        return np.sqrt((3-5*x + x**2 + x**3)) / (x-1)


numbers= [1.1, 1.01, 1.001]
limit(numbers)

But it only outputs one result instead of three outputs from the list: 2.0248456731316713

What did I do wrong with the code above? Thanks!

Upvotes: 0

Views: 62

Answers (4)

AlexisBRENON
AlexisBRENON

Reputation: 3099

The return statement stops the function execution at the first iteration, returning a single value, the computed value for the first item of the input list.

What you are looking for is either a generator, which will return a new value, each time you call the function, or a list comprehension, which will return a new list with the computed values.

You may also use numpy arrays directly as you seem to have it as a dependency (thanks @GIRISH kuniyal for the idea).

import numpy as np

# Generator
def limit_generator(x_es):
    for x in x_es:
        yield np.sqrt((3-5*x+x**2+x**3))/(x-1)

# List comprehension
def limits(x_es):
    return [np.sqrt((3-5*x+x**2+x**3))/(x-1) for x in x_es]

# Numpy arrays
def numpy_limits(x_es):
    x = np.array(x_es)
    return np.sqrt((3-5*x+x**2+x**3))/(x-1)

if __name__ == '__main__':
    numbers = [1.1, 1.01, 1.001]

    generator = limit_generator(numbers)
    print(next(generator), next(generator), next(generator))

    print(limits(numbers))

    print(numpy_limits(numbers))
2.0248456731316713 2.00249843945087 2.000249984482112
[2.0248456731316713, 2.00249843945087, 2.000249984482112]
[2.02484567 2.00249844 2.00024998]

Upvotes: 2

Infinite
Infinite

Reputation: 764

You could use below code ,which fixes the issue

Also note the initial issue in your code is that you are using return in the wrong place as it returns the control to the calling line at first iteration itself.

import numpy as np

def limit(x_es):
    result = []
    for x in x_es:
        result.append(np.sqrt((3-5*x+x**2+x**3))/(x-1))
    return result


if __name__ == '__main__':
    numbers= [1.1, 1.01,1.001]
    limit(numbers)

You could also use list comprehension here , which is a more pythonic way of doing this , look at below code

import numpy as np

if __name__ == '__main__':
    numbers= [1.1, 1.01,1.001]
    #print(limit(numbers))
    list = []
    list = [np.sqrt((3-5*x+x**2+x**3))/(x-1) for x in numbers ]
    print(list)

Upvotes: 0

GIRISH kuniyal
GIRISH kuniyal

Reputation: 770

Hope This code may help you.

   def limit(x_es):
        numbers = np.array(x_es)
        return np.sqrt(3-(5*numbers)+(numbers**2)+(numbers**3))/(numbers-1)

   numbers= [1.1, 1.01,1.001]        
   limit(numbers)

This is easily accomplished using numpy array and its fast too as compare to python list.

Upvotes: 1

ExplodingGayFish
ExplodingGayFish

Reputation: 2897

You are returning the first element, fix it by appending to a list instead

def limit(x_es):
    result = []
    for x in x_es:
        result.append(np.sqrt((3-5*x+x**2+x**3))/(x-1))
    return result   

numbers= [1.1, 1.01,1.001]        
limit(numbers)
#[2.0248456731316713, 2.00249843945087, 2.000249984482112]

Upvotes: 1

Related Questions