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.
The quotes matter. "17" is two characters of text. 17 is a number you can do arithmetic with.
The three conversions, and when you need each one
| You write | It turns | Into | Use it when |
|---|---|---|---|
| int("17") | text | a whole number | ages, counts, marks, years |
| float("2.5") | text | a decimal number | prices, weights, averages |
| str(17) | a number | text | joining a number into a sentence |
Convert once, straight away. Wrap the input() itself, so the variable holds a number from the start:
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.
Read the error message. It names the problem exactly: text (str) and a whole number (int) cannot be joined.
The operators, and the three that differ from pseudocode
| Pseudocode | Python | Example | Gives |
|---|---|---|---|
| + - * | + - * | 3 * 4 | 12 |
| / | / | 7 / 2 | 3.5 |
| DIV | // | 7 // 2 | 3 |
| MOD | % | 7 % 2 | 1 |
| no power sign | ** | 2 ** 3 | 8 |
/ 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.
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 ofprint(Name).Quotes mean "this exact text". Without them, Python looks inside the box called Name.
Changing the capital letters.
Ageandageare 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
+needsstr().Using
int()for a price.int("2.5")stops with an error. Prices needfloat().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(), likeNumber = int(input("Number? ")), so it is not part of the output.
What you do and don’t need
You do need
print(), with text in quotes and variables withoutinput()with a promptint(),float()andstr()- 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+andstr()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+andstr()so you control every spaceround()and formatting decimals: later, when a task needs it
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
input()always gives text. Wrap it inint()orfloat()before doing arithmetic.- When you join a number into a sentence with
+, it needsstr(). Text plus number is an error. //is DIV,%is MOD,**is power.^is not.
Steps 1 to 7
See all 7 stepsBlock A · Values, input and outputProgress is saved on this device.