<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 10/11/2010 09:24 AM, Fasihul Kabir wrote:
<blockquote cite="mid:202409.92397.qm@web114117.mail.gq1.yahoo.com"
 type="cite">
  <style type="text/css"><!-- DIV {margin:0px;} --></style>
  <div
 style="font-family: times new roman,new york,times,serif; font-size: 12pt;">
  <div><span style="color: rgb(0, 0, 191);">a = [0]*5</span><br
 style="color: rgb(0, 0, 191);">
  <span style="color: rgb(0, 0, 191);"> for i in range(0, 4):</span><br
 style="color: rgb(0, 0, 191);">
  <span style="color: rgb(0, 0, 191);">    for j in range(0, i):</span><br
 style="color: rgb(0, 0, 191);">
  <span style="color: rgb(0, 0, 191);">        a[i].append(j)</span><br>
  <br>
why the above codes show the following error. and how to overcome it.<br>
  <br>
  <span style="color: rgb(255, 0, 0);">Traceback (most recent call
last):</span><br style="color: rgb(255, 0, 0);">
  <span style="color: rgb(255, 0, 0);">  File "<pyshell#10>",
line 3, in <module></span><br style="color: rgb(255, 0, 0);">
  <span style="color: rgb(255, 0, 0);">    a[i].append(j)</span><br
 style="color: rgb(255, 0, 0);">
  <span style="color: rgb(255, 0, 0);">AttributeError: 'int' object has
no attribute 'append'</span><br>
  </div>
  </div>
  <br>
</blockquote>
<br>
Well.... What are you trying to do with the append?  If you are trying
to grow the list "a" beyond the original 5 zeros you put into it, then
you need <br>
  a.append(j)<br>
<br>
If you are trying to build a list from the two loops, then don't
pre-fill "a".  Rather start with an empty list<br>
  a = []<br>
and append to it<br>
  a.append(j)<br>
<br>
Don't know what you want, but there is usually not a need to pre-fill
an array like this:  a=[0]*5 .   That looks more like declaring an
array as in C or C++ or many other languages.  And I don't understand
the the "5" in that.  Looking at your loops, I see that 6 passs through
the inner loop are produced:<br>
<br>
 for i in range(0, 4):<br>
   for j in range(0, i):<br>
     print i,j<br>
 <br>
1 0<br>
2 0<br>
2 1<br>
3 0<br>
3 1<br>
3 2<br>
<br>
<br>
In any case.  <br>
   Your "a" is a list, containing integers.<br>
   a[i] indexes the list to produce an integer<br>
   a[i].append(...) attempts to append something to an integer -- an
operation that makes no sense.<br>
<br>
<br>
<pre class="moz-signature" cols="72">-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
</pre>
</body>
</html>