Creating a Program to Decompose a Number and Run a Function on that Decomposition

Fábio Santos fabiosantosart at gmail.com
Thu Jul 18 22:48:23 EDT 2013


On 19 Jul 2013 03:24, "CTSB01" <scott.moore270 at gmail.com> wrote:
>
> Thanks for the alternative links, I'll use gmane.org as an access point
next time.
>
> >
> > Don't paraphrase.  Just copy/paste it into your email message.  And I'm
> >
> > assuming you know to run things from the terminal window, and not from
> >
> > IDLE or something else that messes up the error messages.  Your comment
> >
> > about 'orange' doesn't sound promising.
> >
> >
> >
> > As Ian pointed out, you have no return value in this function.  You
> >
> > calculate something called 'rtn', but never use it.  The last line
> >
> > accomplishes nothing, since rtn is neither assigned nor returned, nor
> >
> > passed nor...   You probably wanted:
> >
> >
> >
> >        return  rtn
> >
>
> Does something like
>
> def phi_m(x, m):
>           rtn = []
>           for n2 in range(0, len(x) * m - 2):
>             n = n2 / m
>             r = n2 - n * m
>             rtn.append(m * x[n] + r * (x[n + 1] - x[n]))
>             print ('n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn)
>           return rtn
>
> look right?
>
> It doesn't seem to have any errors.  However, I do receive the following
error when trying to implement an x after having defined phi:
>
> >>> x = [0, 1, 1, 2, 3]
> >>> phi_m(x, 2)
> Traceback (most recent call last):
>   File "<pyshell#6>", line 1, in <module>
>     phi_m(x, 2)
>   File "<pyshell#2>", line 6, in phi_m
>     rtn.append(m * x[n] + r * (x[n + 1] - x[n]))
> TypeError: list indices must be integers, not float

When you think about it, it makes sense. If you have a list, say,

[2, 5, 1]

You can say, I want the first item (0) or the third item(2) but never, the
one-and-a-halfeth (0.5) item. Python only accepts integer values when
accessing list items.

To access list items, convert your index into an integer value.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130719/7e77da1d/attachment.html>


More information about the Python-list mailing list