Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Non-existent code

how do you make an if then code

1 Like

In which language? The method varies depending…

my code is in python

Then, it would be something like:

    if enemy:
        hero.attack(enemy)
    else:
        # Do something else...or, delete the else clause, to do nothing

If/Then is a statement predominant in Basic. In Python, the ‘then’ is implied…such as:

if there is an enemy:
    then do this

thanks for the help, once again

Thanks for asking! (20 lines of code on the wall…)

Oh, btw, notice the there is no ‘end if’ required either.

how would i do it if i want something to happen when I beat my goal

if thisVariable > thatVariable: # this test to see if the goal has been exceeded; thatVariable = goal
    # here's the code to perform once thisVariable is greater
elif thisVariable < thatVariable:
    # do something else instead to increase thisVariable

Of course, this snippet is assumed to be placed within a loop, where the variables are appropriately refreshed.

so would it be

if “guardian”:
x = 35
y = 18
else:
trap.direction = “horizontal”

Don’t forget to format you code :wink:

Possibly…I read that as:

if there's a guardian:
    x = 35
    y = 18
else:
    trap fires flat

That could be viable code.

ok ill remember next time

i tried my code and the if-else doesn’t work

Ok…how about moving this to the Level Help. Post your code and the issue you are seeing (error pics would be good too, if any). Be sure to include the level name too, please.

This will offer the issue to the rest of the forum, as an actual issue.

so do i ask how to do the if-else code

In the example you just gave you did show the else clause. Just re-post as a Level Help, stating the issue and showing your code/pics…we’ll get you going :wink: