0

I want to call the second function and have it use the result of the first, but am unsure of how to do this. I can't make "array" a local variable as it is dependent on the first function. Can someone please help? Thanks.

def main(n):
    array = []
    for x in range(n):
        array.append(random.randint(0, 5))
    print(array)

def sortarray(list):
    newarray = []
    for i in range(len(array)):
        newarray.append(array.pop.array(array.index(max(x))))
    print(newarray)
1
  • What is list? So you know how to pass argument to function; why not pass array if you need it in a function? (You need to get the returned value from first function before doing so). Commented Mar 4, 2019 at 4:12

2 Answers 2

1

Your first function must return something if you want to pass that result to second function. Please, see the following example:

def main(n):
    array = []
    for x in range(n):
        array.append(random.randint(0, 5))
    return array

def sortarray(array):
    newarray = []
    for i in range(len(array)):
        newarray.append(array.pop.array(array.index(max(x))))
    return newarray

And you could use them like the following:

print(sortarray(main(5)))

BTW, if you're interested in sorting - you definitely should checkout Sorting HOW TO.

UPDATE

That will give the following error:

<ipython-input-1-d0f89860ca87> in sortarray(array)
      8     newarray = []
      9     for i in range(len(array)):
---> 10         newarray.append(array.pop.array(array.index(max(x))))
     11     return newarray

AttributeError: 'builtin_function_or_method' object has no attribute 'array'

As you can see it's pointing to the following line:

newarray.append(array.pop.array(array.index(max(x))))

And the problem is in the array.pop.array call. array.pop is a function.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response. I know there are easier ways to sort, I am trying to come up with some on my own for practice. When i run this, it gives an error message "object has no attribute array", even when the main function returns array?
That's next iteration to update your code. Please, see the updated answer - I will highligh couple of things.
I see. It seems like i've gotten the pop function working now. I just wanted it to pop out the highest number and append it to the new array. newarray.append(array.pop(array.index(max(array)))
Cool! As the next step I would love to suggest you to convert those functions into generators. To play and see the difference.
0

Why you pass variable "list" to funcion sortarray but never use it?

Btw, you should not define your variable as "list". It is reserved word in python. Although it may not cause error, but it will confuse someone who look into your code or mis-link by some IDEs.

This may what you want to implement.

def main(n):
    array = []
    for x in range(n):
        array.append(random.randint(0, 5))
    print(array)
    sortarray(array)

def sortarray(array):
    newarray = []
    for i in range(len(array)):
        newarray.append(array.pop.array(array.index(max(x))))
    print(newarray)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.