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.
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.
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:
open("data.csv") or pandas.read_csv("data.csv")new File("data.csv") or Files.readAllLines(Paths.get("data.csv"))std::ifstream in("data.csv");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.csvanddata.csvare different files.
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 author allowed it, a Use my own file button appears under the list of files.
.txt, .md, .csv, .tsv
or .json, up to 50 KB.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.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.
Check these in order:
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.os.chdir in Python), the file will not be found.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.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.