Main.WhileExample History

Hide minor edits - Show changes to markup

February 04, 2010, at 08:42 PM MST by 71.196.160.210 -
Changed lines 22-23 from:

def mystery(n) :

to:

def mystery1(n) :

Changed lines 27-28 from:
        s += i
to:
        s += i         # this is the same as s = s + i
        i += + 1       # same as i = i + 1
Added lines 31-39:
  1. now how about this one:

def mystery2() :

    n = 1
    while (n > 0) :
        print n
        n += 1      # same as n = n + 1
February 02, 2010, at 01:34 PM MST by 129.82.44.241 -
Added lines 1-31:

(:source lang=python:)

"""Examples of iteration using the while statement"""

def countdown(n) :

    while n > 0 :
        print n
        n = n-1
    print "Blastoff!"

def num_digits(n) :

    """counts the number of digits in a number"""
    count = 0
    while n > 0 :
        count = count + 1
        n = n // 10
    return count
  1. what does this function do?

def mystery(n) :

    i = 1
    s = 0
    while (i < n) :
        s += i

    return s

(:sourceend:)