# [Python] Function definition not working - Gold Rush

#1

Hi,

the following code is created after the in-game-help for function, but it fails with the following message:
`Assining to rvalue`

``````self.distanceToItemThisFunctionHasANameNoOtherHas(item) = function(item):
return self.distance(item)
``````

I need this method to sort an array with `Array.sort(key=...)`.

I assume that I can’t write to self in Gold Rush. But why?

EDIT: Sorry the ultra-long-function name, I was just a bit frustraded and needed a name which is definitly not present in a read-only-state

#2

In Python you sort like `sorted([3, 1, 2])` or `[3, 1, 2].sort()`. More info at https://wiki.python.org/moin/HowTo/Sorting

#3

My plan is the following:

First I get all items and save them in `items`. Then I sort them by their distance to me and approach the next one.
The actual code is a bit more complicated (I don’t check all coins on the field), but for my idea to work I need to sort a given coin-array by distance. According to this thread I can’t use a lambda-function, so I try to create one outside and use it like the following:

``````self.func(n) = function(n) return self.distance(n)

items = self.getItems()
items.sort(key=func)
``````

Now I expect items to be sorted in ascending order by distance. I can now grab the first element and move to it:

``````self.move(items[0].pos)
``````

#4

Some glasses will make this easy with the methods they provide. E.g.

``````nearest_item = self.findNearest(self.findItems())
self.move(nearest_item.pos)
``````

But it’s still possible with the methods you have available. E.g.

``````def compare_distance(item1, item2):
if distance(item1) < distance(item2):
return -1
elif distance(item1) > distance(item2):
return 1
else:
return 0

nearest_items = sorted(self.getItems(), cmp=compare_distance)
``````

The comparison function can be simplified since it needs a positive, negative, or zero value (not necessarily +1, -1, and 0) to check if it’s greater, lower, or equal.

``````def compare_distance(item1, item2):
return distance(item1) - distance(item2)
``````

Sorting enemies by distance (Python)
#5

I copy and paste that line and drop it into my code and Immediately it gives me a “Your parentheses must match” error…
(I changed getItems to findItems still same error)

so I change it to something simple that shouldn’t have problems…

gg = sorted([5,4,3,2,1], cmp=compare_distance)

and I get the same error??? (it doesn’t care if “compare_distance” is defined or not)

#6

I know this is an old topic but I get exactly the same error as Vievo.

``````def compareYpos(unit1, unit2):
return unit1.pos.y - unit2.pos.y

soldiers = sorted(self.findByType("soldier", self.findFriends()), cmp=compareYpos)
``````

how I can solve this?

#7

For the record, I haven’t unlocked the very last world. Could this be the reason?

#8

@Gulzt I believe CodeCombat’s Python compiler does not support keyword arguments (named parameters) yet.

The “your parentheses must match” error message is incorrectly applied to this parsing error — I believe Aether thinks your parentheses don’t match because the error occurs when parsing a function call’s arguments. That is, the keyword arguments syntax is not supported and the compiler fails to parse the function call’s arguments.

The correct solution would be, of course, adding keyword arguments support to Filbert (CoCo’s Python -> JS transpiler).