.
From the condition, we have one equation for total money
x + 2y + 3z = 12 dollars (1)
which should be solved in positive integer numbers x (the number of pensils), y (the number of notebooks)
and z (the number of pens).
At this point, the setup is just completed.
Under given conditions, the problem has several solutions (more than one).
The solution you mention is only one possible of several.
The full list of the solutions is in the Table below
T A B L E
x y z
---------------------------
1 1 3
2 2 2
4 1 2
1 4 1
3 3 1
5 2 1
7 1 1
So, the given answer is only one possibility,
and actually, 6 other are possible, giving the full number of solution as 7.
The way to find all these solutions is well organized "trials and errors" method.
To get the idea of this method, look in the table and see how I go down from the highest possible values of z;
then see how I go down from the highest possible values of y, at given fixed value of z.
It is a kind of a "descend method" with the use of lexicographic (~ alphabetic) ordering of unknowns.
-----------
Solved, explained and completed.