Creating a Program to Decompose a Number and Run a Function on that Decomposition
CTSB01
scott.moore270 at gmail.com
Thu Jul 18 22:54:53 EDT 2013
On Thursday, July 18, 2013 10:48:23 PM UTC-4, Fábio Santos wrote:
> On 19 Jul 2013 03:24, "CTSB01" <scott.m... 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.
Thanks Fabio. Is there a statement that lets me specify that I only need it to take the integer values?
More information about the Python-list
mailing list