Reputation: 1
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)
Upvotes: 0
Views: 63
Reputation: 6657
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.
Upvotes: 1
Reputation: 150
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)
Upvotes: 0