# Class notes for Monday, February 20, 2006

#-----------------------------------------------------------------------
# making decisions

def reciprocal():
    n = input("Enter a number: ")
    if n == 0:
        print "Sorry, zero has no reciprocal"
    else:
        answer = 1.0 / n
        print "The reciprocal of", n, "is", answer


#-----------------------------------------------------------------------
# leap years
#   - multiples of 400 are leap years: 1600, 2000
#   - other multiples of 100 are not leap years: 1700, 1800, 1900
#   - all other multiples of 4 are leap years: 1996, 2004, 1904
#   - nothing else is a leap year

def isLeapYear(year):
    if year % 400 == 0:
        return True
    elif year % 100 == 0:
        return False
    elif year % 4 == 0:
        return True
    else:
        return False

# an alternative approach using and/or operators

def isLeapYear(year):
    if (year % 400 == 0) or ((year % 100 != 0) and (year % 4 == 0)):
        return True
    else:
        return False

# if/elif/else statements can be nested

def leap():
    year = input("Enter a year: ")

    if isLeapYear(year) == True:         # 1

        if year > 2006:                  # 2
            verb = "will be"             # 3
        elif year < 2006:                # 4
            verb = "was"                 # 5
        else:                            # 6
            verb = "is"                  # 7

    else:                                # 8

        if year > 2006:                  # 9
            verb = "will not be"         # 10
        elif year < 2006:                # 11
            verb = "was not"             # 12
        else:                            # 13
            verb = "is not"              # 14

    print year, verb, "a leap year"      # 15

# The order that lines are executed in leap() depends on the
# particular value entered for the year.  Examples:
#
# year        execution sequence
#
# 2000        1 2 4 5 15
# 2003        1 8 9 11 12 15
# 2006        1 8 9 11 13 14 15
# 2008        1 2 3 15
# 2009        1 8 9 10 15
# lines 6 and 7 will never be executed

#--------------------------------------------------------------------
# while loops

# Start with any whole number greater than 1.  If the number is even,
# divide it by two, otherwise triple it and add one.  Repeat this
# process until the number becomes 1.  Question: will all numbers
# eventually reach 1?  Most numbers seem to, but no one has ever been
# able to prove that all numbers will.  If you could prove this, you
# would instantly become famous, at least in the world of mathematics!

def testnum(n):

    if n < 1:
        print "sorry, n cannot be less than 1"
        return

    while n > 1:
        print "n is currently", n
        if n % 2 == 0:
            n = n / 2
        else:
            n = 3 * n + 1

    print "yes, we hit 1"

