[Tutor] Building Starships -- object of type 'int' has no len()

Terry--gmail terry.kemmerer at gmail.com
Fri Aug 15 21:21:17 CEST 2014


Thanks for your response JL.

I added the following Exception to the code snippet:

for line_number in range(len(catalog2)):

for col in range(len(catalog2[line_number])):

try:

if lens[col] < len(catalog2[line_number][col]):

lens[col] = len(catalog2[line_number][col])

except TypeError:

print(catalog2)

print("col #: ", col)

print("line_number", line_number)

print("catalog2 col content: ", catalog2[line_number][col])

print("catalog2 col content TYPE: ", type(catalog2[line_number][col]))

exit()


Below is the exception printout. I ordered the catalog2 printout portion 
to be more readable:

[['Drives', 'Type', 'Price', 'Max Speed', 'Energy Drain', 'Rq-Crew', '', 
'', ''],
  ['Drives', 'Solar Sail', 3, 1, 'None', 2, '', '', ''], 
<<------------------------here's the problem
  ['Drives', 'Bussard Ramjet', '10', '7', '2', '3', '', '', ''],
  ['Drives', 'Fusion', '70', '15', '5', '8', '', '', ''],
  ['Drives', 'Matter-Antimatter', '1500', '145', '15', '13', '', '', ''],
  ['Drives', 'Warp Drive', '2500', '250', '45', '17', '', '', '']]
col #:  2
line_number 1
catalog2 col content:  3
catalog2 col content TYPE:  <class 'int'>

Hmmm. It appears that when I bought a Solar Sail the first time through, 
my code has altered my master Catalog away from holding strings to 
holding integers for that particular row-record, which is why when I 
return to a particular  category to make a second purchase, the code 
errors out when it hits the integers that are suppose to be strings.

You know, for some reason, it just never sank into my thick skull that 
the integer 3 doesn't have a length. I just proved that to myself on the 
python commandline. It's just that my eyes see the number 3 displayed on 
the screen and my blond brain automatically counts it as occupying 1 
space in the printout and thinks it's length is 1....which is why the 
texts say to use str() on integers and floats in mixed printouts, I guess.

Thanks! I guess the above code is actually fine.

Now, I just need to discover how and where I am somehow altering the 
master catalog where everything is suppose to be contained in strings. 
The problem with that is, I don't think there is any code to mess with 
the master catalog. I create catalog2 from scans of catalog each time 
another category is picked. And I store the chosen parts, after a 
quantity has been selected, in a list called myship. So, the problem 
must be where I do the quanity calculation, as that is done in integer 
form before the new row is added to myship. So, I will start there as I 
suspect I didn't switch it back to strings when I altered the catalog2 
row, just before I appended it to myship.

Thanks for putting me back on track!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140815/a07d88a8/attachment-0001.html>


More information about the Tutor mailing list