3 neat ways to use the Grok Terminal

A nifty feature of Grok that many students and teachers don’t know about is Grok’s Python Terminal. It’s a bit of a mystery, so in this blog post I’ll explain how you can use it, and try to dispel the mystery around it!

First off, what is the Terminal? It’s what is often referred to as a “Read Evaluate Print Loop” or REPL. It lets you interactively write a line of Python, execute it, see the result and then enter another line of Python. If that doesn’t really make sense yet, don’t worry, keep reading to see it in practice.

There’s three main ways that the Terminal is useful:

  1. Trying out ideas
  2. Doing live demos
  3. Debugging!

In this post I’ll talk you through some examples that use the Terminal in these three different ways, but there’s lots more you can do with it! It’s an underestimated teaching tool that can really help in the classroom.

1. Trying out ideas

Often you have an idea about how you might solve something, but you’re not sure about how the parts fit together. If you write it anyway and end up with a bug, you can’t be sure why the bug is happening. Did it work differently to how you thought, or is the mistake somewhere else?

The terminal allows you to try things out and see the result. Let’s imagine we need to split an input string like ‘abc$$def$$ghi’ so that we get ‘abc’, ‘def’ and ‘ghi’. Previously we’ve used string.split() to do the same with ‘abc def ghi’, but never with something like this double dollar sign. Can we just pass ‘$$’ in to split and have it work?

Instead of wondering, lets try it out! If you want to follow along at home you can open up our Python Playground, but this will work in any Python course.

First click the Terminal button.

Then you’ll get a prompt that looks like this:

Now we’ll enter in the example we want to try:

Okay, we can see what the return value will be now. It looks like it works how we expected! We could even try some other examples, like an edge case ($$$).

Now we also know how it will behave in this odd scenario! Without checking, this behaviour could have caused unexpected bugs.

2. Live Demos

The terminal is really useful for demonstrating features of python. You can pull it up and start typing Python in to demonstrate it. Lets say we wanted to demonstrate how input works, we can type out a line of code and run it straight away.

You can try more examples to see how it behaves in different contexts, or write lines that follow on from the last.

If you’re explaining to a class on a projector you can also take the terminal full-screen, so you can focus on demonstrating some Python.

3. Debugging

One of my favourite uses of the Terminal is to debug a program you’ve already written. When you press the terminal button we run your program and then open up the terminal. That means you have full access to any variables you’ve created. This can give you visibility in to the “hidden” state of your code.

Let’s run through an example. Let’s say you have just written a program to help with your movie scheduling (from the ACA’s Chatbot course). When users suggest Tuesday, it’s meant to print out “Great! Tuesdays are cheap.” but it doesn’t seem to, and you’re sure you have everything right.

Lets open up the terminal and see whats going on.

The program hasn’t printed out what we expected. Is the value in our day variable right?

Okay, that looks right. How about our if statement? We can copy out the key expression to see if it is True or False.

Well that doesn’t look right! What is going on there? They should be… of course we’ve made a typo; we should be checking for uppercase ‘T’ Tuesday. Let’s see what happens if we fix the typo.

Much better! Now we know how to fix our program, and we figured it all out in one Terminal session.

So that’s a few different ways you can use the Python terminal in Grok. If you have some other neat ideas for things you can do with the Terminal, please tell us!