The problem is in your commandSoldiers()
function. You need an if enemy
in there. Also, you don’t need to define soldier, but you need to only command soldiers, so you need to include a statement to the effect of, if friend.type == "soldier"
. I was able to get your code to work by just changing the last three lines of the commandSoldiers()
function.
firstly, above line 8, write: def commandSoldiers():
then put inside it hero.findByType("soldier")
because we’re trying to get only the soldiers without the peasant
now you should add this for-loop inside the function
for soldier in soldiers:
# find the nearest enemy here then use this if-statement
if enemy:
hero.command(soldier, "attack", enemy)
this should do the first function, once you do that, we move onto the next function
If you use this as helpful. I have made it so that it makes a infinite loop. Because it was a solution and we don’t give solutions out.
i google a video for it and i found the code i will let you know if it works or not.
Sometimes, the videos aren’t correct. Unless it is a newer video, then it may be correct. But still you don’t really learn anything by watchinga video. But if you learn by trying to fix your mistakes with helpof other coders, you tend to learn how to make sure that mistake doesn’t happen again. But you can try the video and then we can see if it worked or not.
There is no commandSoldiers() function
You need to make the commandSoldiers() and pickUpNearestCoin() functions. That is why they are not defined.
Did you switch form Python to JavaScript?
yes i did the person in the video was in java so i needed to switch because i would not be able to type the code
That’s why we don’t use videos. So, now I can’t really help you solve it because I don’t know JavaScript
i can switch back to python
i switched back to python
Use some of the following topic to help you out.
https://discourse.codecombat.com/t/solved-two-flowers-python/15824
https://discourse.codecombat.com/t/solved-two-flowers-python/14403
After you have used those, come back. Those were the two that helped me out the most
Please copy your code and format it correctly. I can’t see the image
def summonSoldiers():
if hero.gold >= hero.costOf(“soldier”):
hero.summon(“soldier”)
Define the function: commandSoldiers
def commandSoldiers():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “attack”, enemy)
Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType(“peasant”)[0]
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
def summonSoldiers():
if hero.gold >= hero.costOf(“soldier”):
hero.summon(“soldier”)
# Define the function: commandSoldiers
def commandSoldiers():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “attack”, enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType(“peasant”)[0]
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
Thanks for the right format. But for me that code works