Showing posts with label while. Show all posts
Showing posts with label while. Show all posts

Tuesday, 28 July 2015

Functions

#!/usr/bin/env python

# Functions allow us to include code in our programs that will only run when
# explicitly called and can be reused.

# Simple function example.
def allStars(n):
        for i in range(0,n):
                print "*"

# Main Program
run = True              
while run:
        x = raw_input("How many stars should I print? ")
        if str.isdigit(x):
                allStars(int(x))
                x = raw_input("Do you want another go? Press Y or N")
                if str.upper(x) == "N":
                        run = False
        else:
                print "%s is not a number." % x

Flow Control 2: while, while-else

#!/usr/bin/env python

# Simple while loop example.
i = 0
while i != 10:
        i = i + 1
        print i
       
# while-else
while i != 10:
        i = i +1
        print i
else:
        print "We're all done here!"