Skip to main content

Run python code by pushing a button

Comments

4 comments

  • Moderator Beacon of Knowledge Super Star

    How are the students adding the forward function? IOW do they add the "def forward():" and the program contents in the same program where they add the:

    player.on_tell_command("forward", forward)
    0
  • Márton Bori

    Yes, the whole program looks like this:

    def forward():
        agent.move(FORWARD,1)

    def back():
        agent.move(BACK,1)

    def left():
        agent.turn_left()

    def right():
        agent.turn_right()

    player.on_tell_command("forward", forward)
    player.on_tell_command("back", back)
    player.on_tell_command("left", left)
    player.on_tell_command("right", right)

     

    0
  • Debbie Alexander
    Moderator Beacon of Knowledge Super Star

    Okay, the code itself works for me in MakeCode Python (not Azure Notebooks)

    And in order to work, since it is moving the agent, the agent has to be present

    And as you say, the user has to have cheats on, and be able to code. I'm assuming the kiddos have the code pasted into Python in Makecode and have run it.

    But a member can break, place and interact, but not code. I think this is your problem. Have your students try to run independent code. I think if they try to run a say command from MakeCode you will see they can't.

    If they can, then we have another problem.

    As a note, if you make this on a chat command they can just say "forward" and the agent will move without all the other typing.

    0
  • Márton Bori

    As a note, if you make this on a chat command they can just say "forward" and the agent will move without all the other typing.

    Yes, thats what we ended up doing, but its just so much more inconvenient, than triggering code by pushing a button.


    But a member can break, place and interact, but not code. I think this is your problem. Have your students try to run independent code. I think if they try to run a say command from MakeCode you will see they can't.

    Thats true, they can't run say command from MakeCode, or any command that executes a normal /command, but they can run simple commands from MakeCode, like moving the agent.  The problem is that there's no indication what so ever of a given MakeCode command's permission level.
    So I never know if the kids did something wrong, or its just that members can't run that specific command.

    Since then I found a work around by the way: If they sign up for the on_block_broken event, it works for members too.
    So instead of having a command block "tell" them a trigger word, I could have the kids destroy a certain kind of block, and always regenerate that block with a repeting command block. They just have to write in the code:

    blocks.on_block_broken(BLOCK_TYPE, method_name)

    And this does work with members too.
    The only problem with this is that for every method I want to trigger this way needs a different type of block, which can quickly become a mess. So it would be nice if the on_tell_command worked as well.

    But hey! At least it works! :)

     

     

     

    0

Please sign in to leave a comment.