astute
astute

Reputation: 57

Creating functions, adding argument to range

I am brand new at programming (just learned what a command line is); no previous experience and just found this website so excuse any formatting errors.

I am really struggling with adding arguments to a function. Specifically, I need to add an argument to a function I created in which I am returning a range with increments of 5. For reasons that are beyond my current skill level (but obvious to you guys, I'm sure), I cannot use the main function to replace the 51 in my fname function with 101. Essentially, I have to give fname an argument for the upper-end of the range. From the main function, I have to pass the value of 101 to the fname function so that fname returns [0, 5, 10, ... , 100] to the main function and the main function prints this to the screen.

def fname(arg1):
    """this function will compute a range from 0 to 50 in increments of 5"""
    result = list(range(0, 51, 5))
    return result


def main():
    arg1 = list(range(0, 101, 5))
    from_fname_function = fname(arg1)
    print(from_fname_function)

if __name__ == '__main__':
    main()

Any help (with thorough explanation) would be greatly appreciated. I have found many resources with how to use arguments in a function, but I cannot figure out how to change the max limit in the range in this way.

Upvotes: 1

Views: 53

Answers (3)

RudRocks05
RudRocks05

Reputation: 13

def fname(start, end, inc):
    return list(range(start, end, inc))

print(fname(0,51,5))

In the above code the function fname returns the list of values from 'start' to 'end' with the increment 'inc'

op: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

Upvotes: 0

user2390182
user2390182

Reputation: 73450

Note that you are not using the argument you have added in the function body. Judging from the docstring, I assume you want to make the upper boundary of the range flexible. Try sth along the following lines:

def fname(arg1):
    # actually use the argument to build the range
    result = list(range(0, arg1, 5))  
    return result

def main():
    arg1 = 101
    from_fname_function = fname(arg1)
    print(from_fname_function)

Upvotes: 2

Ray Toal
Ray Toal

Reputation: 88378

I think the trick you are looking for is to have the function fname to be able to return lists that start at zero and go in increments of 5, up to whatever limit you choose. You are really close; you just need to use the parameter arg1 when you make the range, like this:

def fname(arg):
    return list(range(0, arg, 5))

Now you can use the function to make lots of different lists:

def main():
    print(fname(51))      # this will print [0, 5, 10, ..., 50]
    print(fname(101))     # this will print [0, 5, 10, ..., 100]
    print(fname(10))      # this will print [0, 5]

The idea of function arguments is that they allow you to make a function that can do something with different values at different times. So if you want to make lists starting at zero that have increments of five, and you want to do this for different ending values, then you make the limit be the argument (because that's the part that changes), and use the argument at just the right place in the function.

Hope that helps!

Upvotes: 0

Related Questions