JIT — Intro to Data Science · Python Lab · Problem 4 of 30

Text that looks like a number

input() hands back characters, never numbers — and the programme stops the moment you forget it.

The number that is not a number

A programme that only ever works on marks typed into the file is not much use. You want to ask the person at the keyboard. Python does that with input() — it prints a prompt, waits for someone to type, and hands back whatever they typed.

And there is the trap. Whatever they typed comes back as text, always. Type 72 and you get the two characters 7 and 2, not the number seventy-two. Text cannot be added to, averaged or plotted. Ask Python to add 5 to it and the programme stops.

New word — type Every value has a type: str for text, int for whole numbers, float for numbers with a decimal point. Ask what something is with type(x), and Python answers with a line like <class 'str'>.
New word — converting int("72") builds the whole number 72 out of that text. float("7.5") builds 7.5. str(72) goes the other way. The conversion has to succeed: int("abc") has nothing to build a number from, and stops the programme.

What you are building

A programme that asks for marks, shows the type of what came back, converts it to a whole number, shows the type again so the change is visible, adds 5 grace marks and prints the result. Typing 72 should give:

Enter your marks out of 100: 72 <class 'str'> <class 'int'> With 5 grace marks: 77
Coming later Right now a typing mistake stops the programme dead. Checking the answer first and asking again politely needs a loop, which comes in problems 13 to 15.

Arrange the steps

Six things have to happen. Two are already in place — the note at the top and the question to the person at the keyboard. Click the remaining four in an order that works.

You are trying to make a change visible. That means looking before as well as after.

Your plan

The steps still to place

Fill the statements

Your steps written as Python. Three pieces are missing, and two of them decide whether the addition works at all.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. Instead of typing at a real prompt, put what the person types into the box below.

Your programme

Output

Nothing has run yet.