Python Basics Quiz: Interpreter, Console I/O and Tracebacks

Test your Python basics with 12 questions and three programs on how CPython runs code, the main guard, input and print, exit codes and reading tracebacks.

  • Course: Python study plan
  • Module: Python and the interpreter
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 22 min
  • Runtime: CPython 3.11

Checkpoint — Python and the interpreter is the checkpoint that closes the Python and the interpreter module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the whole module: what CPython is and how it runs a file, the four ways to run Python and the __name__ guard, the parts of a program (statements, expressions, blocks, comments, docstrings, names), the console I/O patterns every exercise uses, and how to read a syntax error and a traceback.

How it works. Twelve questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.

Before you start, make sure you can answer these from memory:

  • What does CPython do with your source before the first line runs, and what stops it?
  • When is __name__ equal to "__main__", and why does a script check?
  • What does input() return, what does it strip, and what does it raise at the end of input?
  • How do you print the elements of a list separated by spaces, and why is print(nums) wrong?
  • Which exit code means success, and what does an uncaught exception do to it?
  • In a traceback, which line says what went wrong and which says where?
  • What is the difference between TypeError and ValueError?

The three programs are a receipt built from records read line by line and printed with fixed decimals, a calculator that reports the exception name instead of crashing, and a word-count utility that reads standard input to the end.

Common questions

What does input() return, and what does it raise at the end of input?

input() returns one line of standard input as a string, with the trailing newline stripped — digits included, so int(input()) is needed for a number. When there is no more input it raises EOFError, which a read-until-end loop catches.

What does an uncaught exception do to a Python program's exit code?

It ends the program with exit code 1 after printing a traceback to standard error. Exit code 0 means success, and it is what a script returns when it reaches the end of the file normally.

In a Python traceback, which line says what went wrong and which says where?

The last line says what: the exception type and its message. The frame just above it says where: the file, line number and function that raised it. The frames above that are the callers that led there, most recent call last.

Exercises

Receipt

Read n purchase records and print a receipt. Each record is a name, an integer quantity and a unit price. Print one line per item and a total, all prices with exactly two decimals.

Input: n, then n lines name qty price. Output: <name> x<qty> @ <price> = <qty * price> per item, then total = <sum>.

2
apple 3 0.5
bread 1 2.25

prints

apple x3 @ 0.50 = 1.50
bread x1 @ 2.25 = 2.25
total = 3.75

A calculator that never crashes

Read lines until the end of input, each meant to be a op b with integer operands and one of the operators + - * / // % **. Print the result of each line — an integer for every operator except /, whose result is printed with two decimals. When a line cannot be computed, print the name of the exception instead of crashing: dividing by zero is ZeroDivisionError; a non-integer operand, an unknown operator, or a line without exactly three tokens is ValueError.

Input: zero or more lines. Output: one line per input line.

7 + 2
7 / 2
7 // 0
7 ^ 2

prints

9
3.50
ZeroDivisionError
ValueError

wc

Write the classic word-count utility. Read all of standard input and print the number of lines, words and characters. Lines are what str.splitlines() yields, words are whitespace-separated tokens, and characters are every character including newlines.

Input: any text, possibly empty. Output: three lines: lines <n>, words <n>, chars <n>.

hello world
foo

prints

lines 2
words 3
chars 16

In this module: Python and the interpreter

← Errors and tracebacks — reading what the interpreter tells you · Numbers — int, float and the arithmetic that surprises →