AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Types, flags and fixed values

Week 9 · 1.5 Data in text-based programs · 3 lessons
3

Fixed values and changing types

Computing 8: Topic 1.5, pp. 106–124
GoalUse a constant instead of a repeated number, and use casting to change a value from one data type to another.
New words
constant · a named value that is set once and must not change while the program runscasting · changing a value from one data type to anotherdeclare · to give a variable or constant its name and first value
Explanation

A constant is a named value that is set once at the start and never changed while the program runs. In pseudocode we write CONSTANT SEAT_PRICE = 12. In Python the rule is to use capital letters, as in SEAT_PRICE = 12. Constants are useful because a price used in ten places can be changed in one place, the name says what the number means, and nobody types a different value by mistake. Casting changes a type. int() makes an integer, float() makes a real number, str() makes a string and bool() makes a Boolean. int(7.9) cuts off the decimal part and gives 7. bool(0) and bool("") are False, but any other number or non-empty text is True, even the text "False". Casting a word to a number, such as int("abc"), stops the program with a ValueError.

Pseudocode
1CONSTANT SEAT_PRICE = 12
2INPUT seats
3total ← seats * SEAT_PRICE
4OUTPUT "Total: ", total
Output
Total: 48
Worked examples
Python
1SEAT_PRICE = 12
2seats = int(input("Seats? "))
3total = seats * SEAT_PRICE
4print("Total:", total)
Output
Total: 48
A theatre uses a constant for the seat price. It is declared once, in capitals, at the top. With 4 seats typed the total is 48. If the price changes to 15, only line 1 needs to change.
ExpressionValueType
int("12")12integer
int(7.9)7integer
float("2.5")2.5real
str(7)"7"string
bool(0)FalseBoolean
bool(5)TrueBoolean
bool("")FalseBoolean
bool("False")TrueBoolean
Casting results. Each row is the expression, the value it gives and the type of that value. The last row surprises many people: any text that is not empty is True.
Python
1n = int(input("Number? "))
2print(n + 1)
Output
ValueError: invalid literal for int() with base 10: 'abc'
Casting text that is not a number stops the program. The error is a runtime error, because Python starts the program and fails on line 1. Python shows this last line when the user types abc.
Class activity

Constants and casts (no computer). Part 1: write a short program for a stall with a constant PRICE_PER_KG = 3 and the weights 2, 5 and 9 kilos. Work out the three totals. Then change the constant to 4 and say how many lines you must change. Part 2: make cards with eight casts, such as int("5"), float("5"), str(5), bool(0), bool("0"), bool(""), int(3.99) and bool("stop"). Write the result under each, then check with an adult or in Python. If you have a computer or tablet, run the stall program with both prices.

Practice
1
Write a Python program for a school trip. Declare a constant COST = 6 (the cost per pupil in coins), ask for the number of pupils and print the total cost.
2
Predict the value and type of each expression.
ExpressionValueType
int("40")??
float(3)??
str(25)??
bool(0)??
bool("stop")??
3
This program stops with a TypeError when the user types 8 and 2. Fix it with casting.
Python
1a = input("First? ")
2b = input("Second? ")
3print(a * b)
Output
TypeError: can't multiply sequence by non-int of type 'str'
4
Give two reasons for using a constant instead of typing the number 12 in several places.