[Chicago] Python Tutorial 4.6 function fib()
Jon Sudlow
jsudlow at gmail.com
Sun Jan 6 08:25:59 CET 2008
Oh this question really irrated me last year in school. In python their is
actually a really slick solution. Mine isn't the 'slickist' but I think its
straight forward enouph for you to understand whats happening.
With fib(n) you can say, if n starts at 0, you need at least two numbers to
get the fibonaci sequence going.
Here is my code:
def fib():
#Print Welcome Message
print "Welcome to the fibanocci nth term lookup utility"
#Get Input
nthTerm = input('Please enter a number greater then 1. ')
#Process the input --^
#Initilize first two values for sequence
first = 0
second = 1
#You have to define the first two values for the sequence, 0 and 1
#these are the values you start with. A temp variable holds
#the resulting addition. Then the first variable is assigned the
#value of the second variable and the second variable is assigned
#the value of the previous addition held in temp. We want one less then
#what range will give us, because we are prohibiting use of fib(0) and
fib(1).
for i in range(nthTerm - 1):
temp = first + second
#sexy double-assignment
first, second = second, temp
#Print the output
print "The result is ", temp
On Jan 5, 2008 10:47 PM, Patrick O'Hara <pohara at virtualmotors.com> wrote:
> uh you did not answer the question why did the def fib(n):
> fail to work
> on python
> or my MacBookPro w/ Leopard
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
--
Jon Sudlow
3225 Foster Avenue
221 Sohlberg Hall
C.P.O 2224
Chicago, Il 60625
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20080106/6b8d7574/attachment.htm
More information about the Chicago
mailing list