Skip to content
Programming Foundations with Python
Path outline

Lesson

Output and values

Print exact text and run a simple Python function.

What a program does

A program is a sequence of instructions. Python reads the instructions and performs them in order. For your first problems, the instruction is to print text. The judge runs your program and compares its output with the expected output. Matching the spelling, spaces, and line breaks is part of solving the problem.

A string is text enclosed in quotation marks. Both single and double quotation marks work, but the opening and closing marks must match. The quotation marks belong to the program. They do not appear in its output.

A complete example

After a one-line printing exercise, problems 1407 and 1408 ask you to define a function called g and call it. A function is a named group of instructions. For now, use this small wrapper and concentrate on the print instructions inside it.

Python 3
def g():
    print("Good morning")
    print("AltoCode")

g()
text
Good morning
AltoCode

The line beginning with def defines the function. Its colon starts the body. The four spaces before each print place those instructions inside the function. The final g() calls it. Defining a function alone produces no output, because the instructions have not been called yet.

Each print finishes with a new line. Two print instructions therefore produce two output lines. To print two words on the same line, put both words in one string. Later you will use print with numbers and variables.

Try a change

Before opening the first exercise, predict what happens if you swap the two print lines. Then predict what happens if you remove the final g(). Thinking about the output before running a program builds the habit you will use for harder problems.

First solve Print AltoCode with one print instruction. For problem 1407, adapt the example to print the required greeting. For problem 1408, use two print instructions and match the two required lines. Submit the entire program, including the function call.

Read the verdict

Accepted means the output passed the tests. Wrong answer means the program ran but its output differed. A syntax error means Python could not understand the program. Check quotation marks, the colon, indentation, and parentheses first. Do not add a prompt such as "My answer is" unless the problem asks for those words.

The judge checks observable output. When the statement asks for a function, follow that instruction even if a shorter program could print the same result. You are practicing the requested construction as well as producing an answer.

Sign in to save your progress.

Sign in

Practice