Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing an argument to a function as an index

Good afternoon, I wanted to ask more experienced people if there is a method for passing an argument to a function as an index. Thank you in advance!!

def planting_plan(rows):
    
    i = list(range(0,10+1,2))
    
    er = i[5] # it is very necessary to pass instead of [5] - [rows], I broke my head how to do it

    return er

print(planting_plan(5))

print(type(planting_plan(5)))
    result:10 <class 'int'>

    expected result:10<class 'list'>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This function is completely useless, since you are not using the argument rows anywhere, so the result will be 10 even if you call it with a number != 5.

If you replace [5] with [rows] then your function will change dinamically based on the value of rows and, like so, you’re passing the index:

def planting_plan(rows):

    i = list(range(0,10+1,2))

    er = i[rows]

    return er

So, passing [rows] instead of [5] is not actually "neccessary", but it would not make much sense to write a function with a fixed value instead of passing a dinamic one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading