[Tutor] Module Review
dn
PythonList at DancesWithMice.info
Wed Apr 12 20:37:54 EDT 2023
Hi,
Am a little confused by the title - where's the (Python) module?
but, let's get on with the question...
On 13/04/2023 04.48, Thurman Hill wrote:
> I’m getting a type error when I put this into the blanks… Please help I’ve been stuck for weeks…
>
> Fill in the blanks to print the even numbers from 2 to 12.
>
> number = range(2,12+1,2) # Initialize the variable
> while number > 0: # Complete the while loop condition
> print(number, end=" ")
> number # Increment the variable
>
> # Should print 2 4 6 8 10 12
>
> Error:
> Error on line 2:
> while number > 0: # Complete the while loop condition
> TypeError: '>' not supported between instances of 'range' and 'int'
The experimentation that will help with this sort of problem can be
most-easily carried-out in the Python REPL - open a terminal*, start
Python, and be able to enter one line of code at a time and have Python
immediately execute it (or tell you where things went wrong).
* please 'translate' if you are an MS-Win user
The error message says it all - of course, if you don't know it all,
then... Yes, they can be quite opaque!
"TypeError...'range' and 'int'
So, what is a type-error? It comes when Python is asked to do something
with two operands of different types that it just can't do, ie trying to
add apples to oranges, as the saying goes.
In this case, the two types are: 'range' and 'int'. The range comes from
line 1, and "int" is short for "integer". Thus, we're talking about
"number" and "0" (resp).
In the REPL, if you type the first line, you can then investigate the
result. If you try:
print( number )
-> range(2, 13, 2)
it appears to tell you what you already know - and exactly what you told
Python you wanted - and the "range()" part tells you that it is both of
type "range" and not an integer.
You can confirm this with:
print( type( number ) )
-> <class 'range'>
In fact, the "range-object" (called "number") is a "collection" of
integers: the "2 4 6 8 10 12" you expect.
However, the code (in the while-condition) is trying to treat it as if
it were a single integer.
You can read-up about range-objects, but the full-fat description is a
bit complicated for beginners.
Have you looked at lists and for-loops? If not, now is the time to
do-so! Lists are THE basic "collection" and are "built-in" to Python!
For-loops are designed to process each element in a collection, one
after the other. Accordingly, are a much better tool than the while
(etc) loop (which won't work anyway in its current form).
At this stage in your Python-learning, you can get-away-with treating
the "number" range, as if it were a list.
To double-check the above, try:
print( list( number ) )
-> [2, 4, 6, 8, 10, 12]
What's happening here is that the range (number) is being turned into a
list, and then the list of integers printed. Exactly what you want?
Once you've had time to read and think about that, it's time to go back
to the REPL and try-out these new ideas...
Once you have things working in the REPL, it is relatively easy to
copy-paste (the good bits!) into a code-file ("script")...
NB I (for one) am having trouble imagining that "number" describes a
list/range of integers. If it were called "even_numbers" for example
(note the plural - because it is a collection of multiple integers),
will such also help you get your head around the ideas?
Come back to us once you've had a chance to experiment...
--
Regards,
=dn
More information about the Tutor
mailing list