Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Strings & Characters Primer

Strings & Characters Primer

Your Turn: A Script that Runs Scripts You Enter

A script that can run Python scripts embedded in strings opens up a lot of possibilities.  Cybersecurity activities rely heavily on a computer-connected micro:bit that radio-transmits strings with commands to a remote cyber:bot.  Since scripts can be embedded in strings, it might be possible for you to update your cyber:bot robot’s script on the fly!  One challenge will be to prevent hackers from doing that to your cyber:bot for their own reasons!

This simple script accepts Python statements that you enter.  After you have entered the last Python statement, type run and press Enter to execute the script. 

Example script: embed_your_turn

  • Enter, name, and save embed_your_turn.  
  • Click the Send to micro:bit button.
# embed_your_turn

from microbit import *

sleep(1000)

s = ""
t = ""

while True:
    s = input("> ")
    if(s != 'run'):
        t = t + s + "\n"
    else:
        exec(t)
        t = ""
  • If the serial monitor isn’t already open, click Show serial.
  • Type this into the terminal:
    print(“Hello!”)
    run
  • Now, try this:
    print(“Hello!”)
    print(“Hello again!”)
    run
  • One more, and make sure to add four leading spaces for each indented statement:
    for(n in range(6)):
        print(“n = “, n)
        sleep(250)
    run

Did each different script you entered execute in the terminal?

 

How it Works

The script starts by creating empty strings named s and script.  The s string receives each line you type, and the script string stores the actual script consisting of all the statements you type.

s = ""
script = ""

In the main loop, the s variable prompts for input from you by displaying a > prompt.  When you type a Python statement and press Enter, that statement gets stored in s.  

while True:
    s = input(“> “)

The if…else… statement checks to see if what you typed does not match the string “run”.  When it does not match, it assumes you have typed a statement and uses script = script + s + “\n” to append your statement and add a newline character to any previous statements you might have typed.  

     if(s != 'run'):
        script = script + s + "\n"

When you do type run and press Enter, the exec(script) function call runs the Python script you typed.  Then script = “” sets the script variable to an empty string so that you are starting over with an empty script.

    else:
        exec(script)
        script = ""

 


Printer-friendly version
Try This: Execute Statements from a String
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress