CSwithZain0 / 28passed
Block A · steps 1 to 7

Values, input and output

0 of 7 passed
The idea

A program runs top to bottom, one line at a time. print() shows something. input() asks for something. A variable is a named box that holds a value until you change it.

That is the whole of this block, apart from one fact that causes more wrong answers than anything else in it: input() always gives you text. Even when the user types a number. Step through this and watch the box change.

Step through itstart
Age
·
Kind
·
Later
·
Type an age in the box, then press Step.
Output

The quotes matter. "17" is two characters of text. 17 is a number you can do arithmetic with.

Reference

The three conversions, and when you need each one

You writeIt turnsIntoUse it when
int("17")texta whole numberages, counts, marks, years
float("2.5")texta decimal numberprices, weights, averages
str(17)a numbertextjoining a number into a sentence

Convert once, straight away. Wrap the input() itself, so the variable holds a number from the start:

convert as you read
Joining text

A plus sign joins text, but only text to text

Between two pieces of text, + glues them together. Between a piece of text and a number, Python stops with an error, because it cannot tell whether you meant to add or to join.

stops with an error
works

Read the error message. It names the problem exactly: text (str) and a whole number (int) cannot be joined.

Arithmetic

The operators, and the three that differ from pseudocode

PseudocodePythonExampleGives
+   -   *+   -   *3 * 412
//7 / 23.5
DIV//7 // 23
MOD%7 % 21
no power sign**2 ** 38

/ always gives a decimal, even when it divides exactly: 6 / 2 is 3.0. If you want a whole number, you wanted //.

^ is not power in Python. With whole numbers it does not crash either. It quietly gives a different number. Powers are **. 9618 pseudocode has no power sign at all: there, ^ is used for pointers.

Watch out

Seven ways this block goes wrong

  • TRAPForgetting int() and adding anyway.

    If both values are text, + joins them: "17" + "5" is "175". No error, just a wrong answer that looks like a number.

  • print("Name") instead of print(Name).

    Quotes mean "this exact text". Without them, Python looks inside the box called Name.

  • Changing the capital letters.

    Age and age are two different boxes. The second one does not exist yet, so Python stops with a NameError.

  • Joining a number without str().

    The TypeError above. Every number joined into a sentence with + needs str().

  • Using int() for a price.

    int("2.5") stops with an error. Prices need float().

  • Leaving out the space at the end of a prompt.

    input("Age?") puts the typing right against the question mark. Add a space: input("Age? ").

  • Printing the question with print().

    Pseudocode writes OUTPUT then INPUT. In Python, put the question inside input(), like Number = int(input("Number? ")), so it is not part of the output.

Scope

What you do and don’t need

You do need

  • print(), with text in quotes and variables without
  • input() with a prompt
  • int(), float() and str()
  • the arithmetic operators, including // and %

Not yet, or not at all

  • decisions: that is block B
  • loops: block C
  • f-strings such as f"{Age}". They work, and you will see them online, but this course joins with + and str() so one way becomes automatic
  • print("Total:", Total) with a comma also works. It adds one space and converts the number for you. This course uses + and str() so you control every space
  • round() and formatting decimals: later, when a task needs it
Exam thinking

The question tells you the exact words to print

When a question says output the total or gives a message in quotes, copy that wording exactly, spaces and punctuation included. Your program is judged on what it prints, so a correct calculation with the wrong words around it can still lose the mark.

Remember these three

  1. input() always gives text. Wrap it in int() or float() before doing arithmetic.
  2. When you join a number into a sentence with +, it needs str(). Text plus number is an error.
  3. // is DIV, % is MOD, ** is power. ^ is not.
Now practise

Steps 1 to 7

See all 7 steps

Block A · Values, input and outputProgress is saved on this device.

Each step is checked against its exact output. Open one to read the task, see a sample run, and write your program. If you get stuck, the task opens the notes that teach it.

  1. 1Say helloPrint your own name on one line and your school on the next.Open
  2. 2GreetingAsk for a name, then print Hello followed by that name.Open
  3. 3Two numbersAsk for two whole numbers. Print their sum, their difference (the first number minus the second, so it can be negative) and their product.Open
  4. 4Your age, forwards and backAsk for an age. Print it now, in 5, 10 and 15 years, and 5, 10 and 15 years ago.Open
  5. 5Shopping totalAsk for a price and how many are bought. Print the total. The price can have a decimal point, such as 2.5, so use float() for it.Open
  6. 6Your sibling at 50Ask for your age and your sibling's age. Print how old the sibling will be when you turn 50.Open
  7. 7The year you turn 100 StretchAsk for the year you were born. Print the year you turn 100.Open

On a phone? Read the tasks and plan on paper here. The coding is easier on a laptop, where the same link opens the same step.

When you want more than practice

The steps are the front door. The class is where it clicks.

Join a class →Ask on WhatsApp