Running Code in Lessons (Try It)

Some lessons have Try It blocks: a small code editor inside the lesson where you can change the code and run it as many times as you like. Running a Try It block is practice, nothing is graded and nothing is counted against you. Some Try It blocks come with data files that your code can read, and some let you run your code on a file of your own.

How do I run code in a lesson?

  1. Open the lesson and find the Try It block. It shows some code in an editor.
  2. Change the code if you want to, then click Run. The output (or the error) appears under the code.
  3. If the program reads typed input (for example with input(), Scanner(System.in) or cin), type that text into the Input (stdin) box before you click Run.

Your changes are not saved. Reloading the page puts the original code back.

How do I read a data file in my code?

When the author of a Try It block attached data files, a box called Files your code can read appears under the code. It lists each file with its size. Click View to see what is inside (the first 2 KB is shown).

When you click Run, every listed file is placed next to your program, so your code can open it by its name. For example, if the list shows data.csv:

  • Python: open("data.csv") or pandas.read_csv("data.csv")
  • Java: new File("data.csv") or Files.readAllLines(Paths.get("data.csv"))
  • C++: std::ifstream in("data.csv");
  • C: fopen("data.csv", "r")

There are two ways to get a file's text into your program: open it by name as shown above, or use Use as input to type it into the Input box for you (next section).

The name must be spelled exactly as shown, capital letters included. Data.csv and data.csv are different files.

How do I use a file as typed input?

Some programs do not open files at all. They read what is typed in, using input(), Scanner(System.in) or cin. For those programs click Use as input next to a file. The text of the file is copied into the Input (stdin) box, and you can edit it before you click Run.

  • If the Input box was empty, the file's text goes in straight away.
  • If you had already typed something, the file's text replaces it and a short Undo appears so you can get your own text back.
  • The file is not moved. Code that opens it by name still finds it.
  • A file that is longer than the Input box accepts (150 KB) cannot be copied in. You will see a message.

How do I run my code on my own file?

If the author allowed it, a Use my own file button appears under the list of files.

  1. Click Use my own file and choose a plain text file from your computer: .txt, .md, .csv, .tsv or .json, up to 50 KB.
  2. The page says which name your code can read it under (for example input.txt). Open that name in your code, exactly like a data file. If the author attached a file with the same name, yours replaces it for your runs.
  3. Click Run. You can also click Use as input next to your file.
  4. Click Clear to remove your file.

Your file stays in your browser until you run. When you click Run it is sent only to run your code. It is not shown to other learners and it is not part of the lesson. Your file is also removed when you click Clear, switch to another language version of the block, or leave the page. Do not use a file with personal information in it that you would not want to be handled like your code.

My code cannot find the file. What is wrong?

Check these in order:

  1. The name. Use exactly the name in the list, including capital letters and the dot and extension.
  2. A plain name only. Use open("data.csv"), not a full path such as C:\Users\me\data.csv or /home/data.csv, and not a folder such as files/data.csv. Files are placed next to your program, and only a plain name finds them.
  3. Do not change folders first. If your code changes the working folder before it opens the file (for example with os.chdir in Python), the file will not be found.
  4. Do not take the name from the command line. Programs run without command line arguments, so the file name has to be in your code.
  5. In Java, keep the class with main first. The first class in your code is the one that runs (it is renamed Main for you), and the data files are prepared there. If the word class appears in a comment above it, move that comment below the class. If the files cannot be prepared, the run stops with a message saying so.

My code says the file is not valid text. What do I do?

Data files are plain text. If you save a spreadsheet from Excel or Google Sheets to use as your own file, choose CSV UTF-8 (not "CSV" or "Excel Workbook"). A file saved as CSV UTF-8 starts with an invisible mark that Python's open() shows as a strange first character at the start of the first header. If you see that, open the file with encoding="utf-8-sig":

with open("data.csv", encoding="utf-8-sig") as f:
    print(f.read())

or pandas.read_csv("data.csv", encoding="utf-8-sig"). Files saved with another encoding (for example UTF-16, or Windows-1252 from older Excel) are refused with the message "isn't plain UTF-8 text". Save the file again as CSV UTF-8 and try again.

Who can see my file?

  • A data file attached by the author is part of the lesson. Anyone who can open the lesson can see it, including people who open an open (OER) course without an account.
  • The file you choose with Use my own file is sent only to run your code. Other learners cannot see it.
  • AI features never read the content of data files. See AI on LearnCoding.


Related guides