Fibonacci numbers/program

David C. Ullrich ullrich at math.okstate.edu
Tue Jan 9 13:05:04 EST 2001


In article <93d92t$7si$1 at nnrp1.deja.com>,
  Eric Anechiarico <wedjlok at excite.com> wrote:
>
> > Writing to a file is very easy.  You open the file for writing with
> > fileobj = open(filename,"w"), then you can use its fileobj.write(s)
> > method to write any string s to it -- just build the string s as you
> > wish, e.g., str(anumber)+'\n' to emit the stringform of anumber
> > followed by an end-of-line.
> >
> > As for a GUI, you have various choices, but Tkinter is probably the
> > simplest toolkit to use in your case (on a Mac).  I have no Tkinter
> > on Mac experience (no Mac experience at all, actually) so I'll
> > refrain from any specifics, just in case there are specific issues
> > I'm unaware of...
> >
> > Alex
>
> Thanks for your response, Alex.  I was wondering in what context I
> should use that within the program itself?  I am fairly new to Python
> and have not found very many references in the books that I have read
> that would show the exact sequence on when/where I would put that
> string/command into effect with the code itself.
>
> 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.])

I feel I should try to help here, since I'm the guy who said
someone would help if you asked the question here. I don't know
anything about tkinter. 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.


> then I input FindFibBetween(1, 100) or whatever range for (1, n)
>
> Thank you for your time and assistance.
>
> Eric
>
> Sent via Deja.com
> http://www.deja.com/
>

--
Oh, dejanews lets you add a sig - that's useful...


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



More information about the Python-list mailing list