Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 14 of 14Free to read

Graduation challenge · Python from Zero

Can you combine everything you've learned into one real program?

Course contents

Why you need it

Knowing each tool isn't the same as building something. Real programs mix input, lists, loops, decisions and functions. This lesson walks through planning a program, then asks you to build a grade book on your own. Finish it and you've completed Python from Zero.

The intuition

Plan before you type. Almost every small program has the same shape:

  1. Input: what data comes in, and in what order?
  2. Process: what needs calculating? Give each calculation its own small function.
  3. Output: what exactly must be printed, line by line?

Then build it in small steps and run after each one. Don't write 30 lines and hope.

For the grade book: input = n scores; process = count, average, highest, lowest, a letter for the average; output = five labelled lines.

Definition

Decomposition

Decomposition is breaking a program into smaller functions, each solving one part of the problem.

Incremental development

Incremental development means writing a program in small steps and testing after each step.

Examples

A shopping total, step by step

Try it yourself

Input, process with functions, output. Change the Input box and run.

def read_prices():
    n = int(input())
    prices = []
    for i in range(n):
        prices.append(float(input()))
    return prices

def total_with_discount(prices):
    total = sum(prices)
    if total > 15:
        total = total * 0.9
    return round(total, 2)

prices = read_prices()
print(f"Items: {len(prices)}")
print(f"Total: {total_with_discount(prices)}")
Input (one line per input() call)
3
4.5
2
10
Step by step
  1. L14Input: read_prices() reads n and then n prices.prices = [4.5, 2.0, 10.0]
  2. L9Process: the sum is 16.5.total = 16.5
  3. L11Over 15, so 10% off: 14.85.total = 14.85
  4. L16Output: two labelled lines.Total: 14.85

A letter for a number

Try it yourself

A small function with if/elif, tested on its own before it's used.

def letter(avg):
    if avg >= 80:
        return "A"
    elif avg >= 65:
        return "B"
    elif avg >= 50:
        return "C"
    return "F"

for test in [95, 70, 50, 10]:
    print(test, letter(test))
Step by step
  1. L10Test the function on several values before using it in the real program.
  2. L395: the first return ends the function with "A".95 A
  3. L810: no condition matched, so the last line returns "F".10 F

Common mistakes

Writing everything at once

Build one piece, run it, then add the next. When something breaks you'll know exactly which part.

Output that is almost right

Graders compare text exactly: Average:71.7 is not Average: 71.7. Copy labels carefully.

Quick quiz

1. What does this print?

def stats(xs):
    return min(xs), max(xs)

low, high = stats([4, 9, 1])
print(high - low)
  1. A
    8
  2. B
    5
  3. C
    -8
Show the answer

Answer: A

A function can return two values; here 1 and 9, so 9 − 1 = 8.

2. What does this print?

print(round(71.666, 1), round(80.0, 1))
  1. A
    71.7 80.0
  2. B
    71.6 80
  3. C
    72 80
Show the answer

Answer: A

round(x, 1) keeps one decimal place and rounds: 71.666 → 71.7. 80.0 stays 80.0.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictTrace a whole program

    Type exactly what this program prints.

  2. ModifyAdd a discount rule

    Change total_with_discount: over 15 still gets 10% off, but over 50 gets 20% off. Keep round(..., 2).

  3. WriteGraduation: grade book

    Read n, then n whole-number scores. Print exactly five lines:

    Count: 3
    Average: 71.7
    Highest: 90
    Lowest: 55
    Grade: B
    • Average is rounded to 1 decimal place with round(avg, 1).
    • Grade is for the average: A for 80+, B for 65+, C for 50+, otherwise F.
    • Write at least one function of your own (for example letter(avg)).

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Decomposition分解Breaking a program into smaller functions that each solve one part.
Incremental development增量开发Writing a program in small steps and testing after each one.
round()round() 四舍五入A built-in that rounds a number to a given number of decimal places.