MTH1W • CODING LESSON 2

Pseudocode

Loading Python…
Task 1

A note Python skips

1. Read, predict, and run

A comment is a note for a person. Python skips a line that starts with #. Read the code, predict the output, then run.

Output
Press Run.

3. Now modify it

Add a comment that says the 4 is a number of squares.

Keep print(4).

The output should still be 4.

Output
Press Run.
Task 2

A plan, then Python

1. Read, predict, and run

Pseudocode is a plan written in ordinary words. Python does not run the plan. It runs the program you write from the plan. Read the plan, predict the output, then run.

Pseudocode:
Set x to 4
Output x
Output
Press Run.

3. Now modify it

The plan now says Set x to 7.

Change that line.

Leave the Output line.

The output should be 7.

Pseudocode:
Set x to 7
Output x
Output
Press Run.
Task 3

Put the plan in order

Click the steps in the order the plan must run. answer cannot be calculated before x is set.

Your order

    Task 4

    Run the plan

    1. Read, predict, and run

    Set means =. Output means print. Changing x recalculates 4 × x. Read the plan, predict the output, then run.

    Pseudocode:
    Set x to 3
    Set answer to 4 × x
    Output answer
    Output
    Press Run.

    3. Now modify it

    The plan now says Set x to 5.

    Change that one line.

    Keep the other lines.

    The output should be 20.

    Pseudocode:
    Set x to 5
    Set answer to 4 × x
    Output answer
    Output
    Press Run.
    Task 5

    Ask for the value

    1. Read, predict, and run

    Input stores the number that is entered. Choose a number for x. Predict 4 times that number, then press Run. When the program asks, type that number and press Enter.

    Pseudocode:
    Input x
    Set answer to 4 × x
    Output answer
    Output
    Press Run.

    3. Now modify it

    • The program below still sets x to 3.
    • Replace the first line with x = int(input("Enter x: ")).
    • Keep the answer line and the Output line.
    • When the program asks, type a number and press Enter.
    • The output should be 4 times that number.
    Output
    Press Run.
    Task 6

    Text or a number

    1. Read, predict, and run

    input() stores text. int() turns that text into a whole number. This program does not use int(). Choose a number, predict what 4 × that text will print, then press Run. When the program asks, type that number and press Enter.

    Pseudocode:
    Input x
    Output 4 × x
    Output
    Press Run.

    3. Now modify it

    • Change the line to x = int(input("Enter x: ")).
    • Keep print(4 * x).
    • The result is 4 times the number entered.
    Output
    Press Run.
    Task 7

    Make a decision

    1. Read, predict, and run

    A conditional chooses which steps run. If the condition is true, the if part runs. If it is not true, the else part runs. This conditional outputs yes when 4 × x equals 12, and no when it does not. In Python, == checks whether two amounts are equal. Predict the output, then run.

    Pseudocode:
    Set x to 3
    Set answer to 4 × x
    If answer equals 12 then
        Output yes
    Else
        Output no
    End if
    Output
    Press Run.

    3. Now modify it

    • Change the if line so it checks whether answer equals 8.
    • Leave x = 3 and the answer line.
    • Keep Else and both outputs.
    • The output should be no.
    Output
    Press Run.
    Task 8

    Test any guess

    1. Read, predict, and run

    Input supplies the guess. The decision outputs yes when 4 × x equals 12, and no when it does not. Predict yes or no for the number you will enter, then press Run. When the program asks, type that number and press Enter.

    Pseudocode:
    Input x
    Set answer to 4 × x
    If answer equals 12 then
        Output yes
    Else
        Output no
    End if
    Output
    Press Run.

    3. Now modify it

    • Keep the Input line, the answer line, and Else.
    • Change the if line so it checks whether answer equals 20.
    • When the program asks, type a number and press Enter.
    • The output is yes when 4 times that number is 20, and no otherwise.
    Output
    Press Run.
    Task 9

    Square a number

    1. Read, predict, and run

    The square of a number is that number multiplied by itself. In the plan, write n squared. In Python, n ** 2 means n squared. Predict the output, then run.

    Pseudocode:
    Set n to 5
    Set answer to n squared
    Output answer
    Output
    Press Run.

    3. Now modify it

    • Keep n = 5 and answer = n ** 2.
    • Replace the print line with an if/else that says whether the square is odd or even.
    • A number is even when the remainder of answer divided by 2 is 0.
    • Python uses % for the remainder.
    • If answer % 2 == 0, output even.
    • Otherwise output odd.
    • The output should be odd.
    Output
    Press Run.
    Task 10

    A linear relation

    1. Read, predict, and run

    A linear pattern can be written with a rate of change and an initial value. The number of tiles is rate × figure + initial value. Predict the tiles in figure 4, then run.

    Pseudocode:
    Set rate to 3
    Set initial to 2
    Set figure to 4
    Set tiles to rate × figure + initial
    Output tiles
    Output
    Press Run.

    3. Now modify it

    The plan now says Set figure to 1.

    Change that line.

    Keep the rate, the initial value, and the tiles line.

    The output should be 5.

    Pseudocode:
    Set rate to 3
    Set initial to 2
    Set figure to 1
    Set tiles to rate × figure + initial
    Output tiles
    Output
    Press Run.
    Task 11

    Check your understanding

    Answer all seven questions, then select Mark Answers. You need all seven correct to finish the lesson.

    1. What does a line that starts with # do?
    2. Which Python line carries out Output answer?
    3. Which Python line carries out Input x?
    4. When does Else run?
    5. A later step uses x. Where does Set x belong?
    6. Which Python line is n squared?
    7. Which Python line is the linear relation?