[SOLVED] If else Problems, Help!

This is just my latest permutation of code, I can’t get else statements to work, and keep getting this error, help!

I’m going to need more to go on than this if I’m going to help you. Can you share what you have so far and the level you are working on. Be sure to use [code ] tags to get your code looking pretty.

Sorry, I thought I enclosed a screenshot on that first post. New here, sorry.

OK, so I see and extra ; in the middle of your if statements. if statements should not have semicolons.

Next, an else statement should be by itself with nothing else:

else{
not
else enemy{

Thanks. the page reloaded and I rewrote my code. This helped.

im stuck on world two level 15b Backwoods Standoff B im really stuck becuase i used else statement and it does not work this is code:

while(true) {
var enemy = hero.findNearestEnemy();
if (hero.isready(“cleave”)) {
hero.cleave(enemy);
else {

{

}
hero.attack(enemy);
}
}

i really dont know whats wrong

HI @Damian_ArchuletaMedi, welcome to the codecombat discourse! :tada:
Please could you format your code so I can see the indentations, here’s a topic explaining how to format:

Thanks
Danny

I am having troubles with my else statement, Its the backwoods standoff level, here is my code while True:
enemy = hero.findNearestEnemy()
# Use an if-statement with isReady to check “cleave”:
if enemy:
ready = hero.isReady(“cleave”)
# Cleave!
hero.cleave(enemy)
else:
hero.attack(enemy)

Welcome to the forum @Carlos_Delgado! :partying_face:
Can you post your code as it says in this?
And can you send me a link?

Lydia

1 Like

Hi @Carlos_Delgado, it would be good to format your code as @Lydia_Song song said, but I can actually see the problem anyway.
I’ve got a few questions from your code which might help you solve it. Does Cleave have a cooldown? Have you checked if your enemy is ready to cleave? (remember you need a if statement to actually check if something is true). When would the else actually run? (Look at what the if statement actually says: if enemy. When won’t there be an enemy? Never, so the else won’t run. Also inside the else statement, what if there isn’t an enemy? A lot of questions there…

Danny

1 Like

I fixed it, it took me a bit of thinking, but I finally understand, I am learning how to code, and this is just one of my more, fun, ways of doing some learning.

1 Like

Nice. If you have any more troubles feel free to make another topic.
Danny

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.