Cant figure out error (python)


#1

i cant figure out what is wrong and it keeps saying "Assigning to rvalue"
i just cant make my guy do what i want when their is a sand-yak


my code is:

flag = self.findFlag(“green”)
while True:
thrower = self.findNearest(self.findByType(‘ogre’))
shaman = self.findNearest(self.findByType(‘shaman’))
fangrider = self.findNearest(self.findByType(‘fangrider’))
headhunter = self.findNearest(self.findByType(‘headhunter’))
sand-yak = self.findNearest(self.findByType(‘sand-yak’))
if thrower:
self.attack(thrower)
if shaman:
self.attack(shaman)
if fangrider:
self.attack(fangrider)
if headhunter:
self.attack(headhunter)
if sand-yak:
self.moveXY(42, 78)

else:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        self.attack(enemy)

#2

I don’t think you can use - in your variable names. The computer is probably trying to subtract sand from yak. Rename your variable sandYak and see if it works.


#3

then i cant write the name and space does not do anything
still says rvalue


#4

No he’s right, the dash is seen as an operator here.
change variables to sandYak or sandyak will work.

sand-yak = self.findNearest(self.findByType(‘sand-yak’))
becomes
sandYak = self.findNearest(self.findByType(‘sand-yak’))

and
if sand-yak:
if sandYak

The name/unit type in findByType(‘sand-yak’)) is left be.

If you’re indiscriminately attacking the nearest member of team ogre.
It might be easier to do a single set:

if !sandYak
self.attack(target)
elif sandYak
self.moveXY