Fibonacci numbers/program

Eric Anechiarico wedjlok at excite.com
Tue Jan 9 14:32:22 EST 2001


In article <93fjsd$1cj$1 at nnrp1.deja.com>,
  David C. Ullrich <ullrich at math.okstate.edu> wrote:
> > Basically, I am defining the command to
> > def FindFibBetween(A,B), having it go through the simple sequence of
> > getting the numbers, and then they output them to a screen.  I want
to
> > interven so that it will NOT print them to a screen, but dump them
> > directly into a text file.
> >
> > So what I need to know is at what point to I call it in the program
to
> > open a file, write the numbers to it, and then close the file?
> >
> > Here is the complete code that I use so far:
> >
> > def FindFibBetween(A,B):
> >   x, y = 1L, 1L
> >   while x < B:
> >     if x >= A: print x
> >     x, y = y, x+y
>
> What a clever function. (Did I really write that? There's an
> unused addition at the end of the loop, kinda stupid. [Added
> a minute later: Otoh when I try to fix that it seems like I
> get a more complicated test condition at some point, so
> _maybe_ it's not so dumb - the fixes that are obvious
> to _me_ add O(number of iterations) to the running time.])
>
> But the following may help:
>
> You need to specify more clearly _exactly_ what you want to do.
> (You want the program to write the numbers to a file taking
> A, B, and the filename from parameters on the command line?
> You want to have the user click a button and then a dialog
> pops up where the user enters A, B and the filename? You
> want to have a few text boxes in a window where the user
> types A, B and the filename, then you save the numbers to a
> file when the user clicks a button? You want what, exactly?)
>
> You also need to specify more clearly how much experience you
> have with tkinter. (And with programming in general, for
> that matter.)
>
> The Python part is easy. You could do something like this:
>
> def FibsBetween(A,B):
>   '''Returns a Python list of the
> Fibonacci numbers between A and B,
> including A (possibly) but not B'''
>   x, y = 1L, 1L
>   fibs=[]
>   while x < B:
>      if x >= A: fibs.append(x)
>      x, y = y, x+y
>   return fibs
>
> def SaveListOfNumbersToFile(numbers, filename):
>   f=open(filename, 'w')
>   for number in numbers:
>     s=str(number)
>     if s[-1]=='L': s=s[:-1]
>     f.write(s +'\n')
>
> Now one way or another (this is the tkinter part,
> and also the part where you have not said exactly
> what you want to do and when you want to do it) you
> determine A, B and the filename. Then you say
>
> SaveListOfNumbersToFile(FibsBetween(A, B), filename)
>
> and you're set.
>

David,

Really, I am not completely even worried about the interface part, since
I can just save everything in a text file and then drop it into the
interpreter and enter in the required arguments from there...Mac's do
not have a command line, as does UNIX and DOS based machines, which is
unfortunate for Mac.

Anyway, exactly what I want to have happen is that I have the range of
number defined in the FindFibs(A,B) part, then I would simply like it to
return the values between those numbers stated, but _not_ to a screen,
instead writing them to a file.

That is basically all that I wish to do, in summary.  Give it the
numbers, have it write the response output to a file to view later.  The
main reason I want to do this is because when the output it gives me
comes on screen, if it is a large amount of digits, they tend to fill up
the buffer on the screen and I cannot scroll back to the beginning and
copy/paste them all because they are no longer there.

I hope that clarified it a little bit better.<g>  I do not have very
much experience in programming at all and am trying to get on the road
with Python. I have attempted to study C in the past and found it was
not worth the effort on my part and its complexitiy.

Thank you,
Eric


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list