[Tutor] Building Starships -- object of type 'int' has no len()
Joseph Lee
joseph.lee22590 at gmail.com
Fri Aug 15 20:05:24 CEST 2014
Hi,
My thoughts are in the message:
----- Original Message -----
From: Terry--gmail <terry.kemmerer at gmail.com
To: Python_Tutor -- Mailing List <tutor at python.org
Date sent: Fri, 15 Aug 2014 11:46:49 -0600
Subject: [Tutor] Building Starships -- object of type 'int' has
no len()
Python 3.3
This has something to do with the nature of FOR statements and IF
statements, and I am sure it must be a simple mistake...but I
seem to be
stumped.
I am writing a starship encounter program as my first real python
program....where the user gets a random amount of credits to
design his
own ship and the computer also gets a random number of credits to
spend
and designs it's own ship, and then they go into combat against
each other.
This is part of the design phase for the user. The user has
picked one
category of the catalog, such as:
Hulls, Drives, Shields, Weapons, Personnel, etc
catalog and catalog2 are lists of lists: [[],[],[]]
OK. So, my program has scanned the master catalog for a
particular
chosen category and has built catalog2 containing all the parts
of that
one category and their associated differences (rows), and now I
want to
print it out as a menu for the user to pick what part to add of
this
particular category to his ship.
But first, so that I can pretty print the menu items and their
associated capabilities to the screen in nice uniform columns, I
need to
know the maximum size of what is going to be printed in each
column on
the screen in advance, so I do the below learning scan through
catagory2
and I build the simple list 'lens' to contain the max size of
each column.
As I run the program, this works perfectly for every NEW category
the
user picks, and the rows of associated data are printed just like
I
want.......*UNTIL the user chooses to buy a second part from a
category
he has already visited*, and at that point things error out.
Below is
the problem code and the error:
lens = []
# pre-format the list called lens for maximum number of columns
contained in catalog2
lens = [0] * len(catalog2[0])
# map the largest sizes of each column into list 'lens'
col, line_number = 0, 0
for line_number in range(len(catalog2)):
for col in range(len(catalog2[line_number])):
*if lens[col] < len(catalog2[line_number][col]):*
lens[col] = len(catalog2[line_number][col])
Traceback (most recent call last):
File "encounter.py", line 379, in <module
myship = designShip(credits, myship)
File "encounter.py", line 354, in designShip
hull, myship, credits, section = nicePrint(hull, credits,
myship,
catalog, section)
File "encounter.py", line 199, in nicePrint
if lens[col] < len(catalog2[line_number][col]):
JL: Hmmm, what is the type of the item stored at that location?
Based on the surrounding code, what you're telling me is that you
wish to know the length of that particular line/column location,
and if it is not a sequence (list, string, etc.), Python will
throw that error. So it appears to me that you're attempting to
use something that the type of the item doesn't support.
TypeError: *object of type 'int' has no len()*
I don't get it. This code does the same job, again and again
successfully as I proceed to pick new categories. Why would it
care if I
want to go over the same ground more than once??? It seems to me
that
the FOR STATEMENT automatically zeros the counters 'col' and
line_number' when it starts counting, but why would it suddenly
choke
because of going over old territory???
In trying to debug i have checked catalog2 and it's data is
present and
intact. I added the line 'col, line_number = 0, 0' in the
sequence of
events although I don't see why it would be needed, and evidently
it
isn't needed because the problem persists. What's left to check?
I really want to buy more guns and bombs and armor! But the
program
seems to be on the side of gun control!!!
:)
Thanks for your thoughts and suggestions!
Cheers,
Joseph
More information about the Tutor
mailing list