fiboeven.py

#!/usr/bin/env python
#
# Printing the first 10 even Fibonacci numbers
#
# Author:  Yotam Medini  [email protected] -- Created: 2005/May/30
#

# Because                sys.stdout.write(...)
# is more mature than    print
import sys

sys.stdout.write("Welcome to Fibonacci-Even Show!\n")

n = 0   # Here we count the even Fibonacci numbers.
f1 = 0  # 1st Fibonacci number
f2 = 1  # 2nd Fibonacci number

while n < 10: 
    if (f1 % 2) == 0:  # Test if f1 is even

        n = n + 1      # We have a new Fibonacci even number - count it

        # Format output. We want to have:
        #            Fibonacci even number[n] = f1
        # and not forget the end-of-line. So 
        fibo_show = "Fibonacci even number[%d] = %d\n" % (n, f1)  # format it
        sys.stdout.write(fibo_show)                               # print it

    # We are out of the 'if'-part above
    f3 = f1 + f2    # The 'future' Fibonacci number
    f1 = f2         # Old f1 value is not needed anymore
    f2 = f3         # Now cycle up to the 'while' statement

# We are out of the while-loop
sys.stdout.write("Hope you enjoyed the Fibonacci-Even Show!\n")
sys.exit(0)


Generated by GNU enscript 1.6.4.