Skip to content
The AI Agent Factory

13.3 Values, types and variables

Approved by
Panaversity ·
12 min read

In everyday life. The characters 4-8-2-5-0-0 can be an amount, a postal code or a part number. You add amounts. You never add postal codes. What you may do depends on what kind of thing they are, not on how they look.

What you will learn. How to make a variable, what kinds of value it can hold, and how to spot text that only looks like a number.

Why it matters. A number read from a file is only text until the code turns it into a number, so check that the code does this before it adds anything.

How do I make a variable?

With a name, an = and a value. The name is a label on a folder, and the value is what the folder holds. The = means "store this value under this name".

LanguageHow the label is written
TypeScriptThe first word says how. const is ink: the name cannot be given a new value. let is pencil: it can.
PythonThe name and = are enough, with no first word. Every label is in pencil.

Two things help you read the code below. A comment is a note for people, which the program skips. In the code, the lines that start with // or # are comments. And a print with two values prints both on one line. Predict what each one prints.

// const, in ink: it cannot be
// given a new value
const terms = "Net 30";
// let, in pencil: it can
let total = 0;
// read total, add to it,
// and store it again
total = total + 482500;
console.log(terms, total);

Both print:

Net 30 482500

total started at 0. The third line read it, added 482500, and stored the result under the same name.

What kinds of value are there?

Five do most of the work.

Kind of valueTypeScriptPythonBrightline example
Textstringstr"INV-8301", "Net 30"
Whole numbernumberint482500 cents
Decimal numbernumberfloatNever used for money
True or falsebooleanboolIs the invoice open?
Nothingnull or undefinedNoneA record with no due date at all

These are the words a type label uses, such as cents: number or cents: int. An agent follows the labels you give it, so you choose them before it writes.

Here are four of the five kinds, in one invoice. Predict what it prints.

// text
const number = "INV-8305";
// a whole number
const cents = 735000;
// true or false
const isOpen = true;
// nothing: it has no due date
const dueDate = null;
console.log(number, cents, isOpen, dueDate);
INV-8305 735000 true null
LanguageHow it writes true, false and nothing
TypeScripttrue, false and null, with no capital letter. undefined also means nothing.
PythonTrue, False and None, each with a capital letter.

Why is an amount from a file still text?

Everything a program reads from a CSV file arrives as text, including the amounts. "482500" is six characters, not a number, until your code converts it.

The fourth line of the code below converts text to a number before it adds. Predict each line, and what happens on the last one.

// text, as a file gives it
const fromFile = "482500";
// text
const other = "61875";
// text + text
console.log(fromFile + other);
// Number(...) turns text into
// a number: number + number
console.log(Number(fromFile) + Number(other));
// number + text
console.log(0 + fromFile);
48250061875
544375
0482500

What + does depends on the kind of value, as with the amount and the postal code. On two strings it joins them, like two postal codes written side by side. On two numbers it adds. On the last line, a number plus text, the two languages differ. They also differ on text that cannot be converted, such as an amount with a comma in it.

LanguageA number plus textText with a comma, "4,825.00"
TypeScriptIt turns the 0 into text and joins, with no error.Number("4,825.00") gives NaN, which means "not a number". The program keeps running, and every total that uses it is NaN too.
PythonIt stops with a TypeError. The lines from Traceback down are Python's error report, and the last line says what went wrong.int("4,825.00") stops with a ValueError.

A program that keeps running with a wrong value is the dangerous case. So in TypeScript, look hard at every place where text becomes a number.

Why whole cents?

Because decimal numbers can be very slightly off. A computer does not store most of them exactly. Predict what each one prints.

console.log(1.10 + 2.20, 110 + 220);

Both print:

3.3000000000000003 330

The whole numbers came out exact. So this book keeps every amount in whole cents, $48.25 as 4825, and turns cents into dollars only when it prints them. Never let an agent keep money in an ordinary decimal number.

Try it

The file in 13-3 is the text-or-number program. Run it and investigate, as in 13.1.

Modify. Real exports often write an amount as "4,825.00". Send: "Change 482500 to 4,825.00 in the file. Don't run it yet." Write down what each line will print. Then ask the agent to run it.

Make. Send: "Make a new file in 13-3 called mine that keeps two invoice amounts as whole cents in variables, adds them, and prints the total. Don't run it yet." Check the total yourself, then ask the agent to run it.

What you should see. It depends on your language.

LanguageAfter your change
TypeScriptIt keeps going: the joined text, then NaN where it converts the amount.
PythonIt stops at its second print, with a ValueError, because "4,825.00" is not a whole number.

The operating rule: convert text from a file as soon as the code reads it, keep money in whole cents, and label the types so a checker can check them.

Figure 13.3. The same characters as text and as numbers. As text, "482500" plus "61875" joins into "48250061875". As numbers, 482500 plus 61875 is 544375 cents, which is $5,443.75. Converting checked text with Number() first gives 544375.

Figure 13.3. Text or number. The characters are the same. The type decides what plus means.

Next. 13.4 shows how a program decides what to do with each invoice.

Check yourself

Question 1 / 9

0 answered

fromFile is "482500" and other is "61875". What does fromFile + other print?

Owner
Panaversity