CSC111 Programming Examples Week 5

From dftwiki3
Revision as of 08:32, 26 February 2015 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Programs developed in class in Week #5= <br /> <source lang="python"> # sing.py # D. Thiebaut # Example of top-down design, and use of functions. # CSC111 week #5...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 08:32, 26 February 2015 (EST)


Programs developed in class in Week #5


# sing.py
# D. Thiebaut
# Example of top-down design, and use of functions.
# CSC111 week #5

# singHappyDave: sings happy birthday to just Dave
# (This function is very boring, as it always does the same
# thing)
def singHappyDave():
    print( "Happy birthday to you," )
    print( "Happy birthday to you," )
    print( "Happy birthday, dear Dave" )
    print( "Happy birthday to you," )
    
# singHappyDave: sings happy birthday to just Sutart
# (This function is also very boring, as it always does the same
# thing)
def singHappyStuart():
    print( "Happy birthday to you," )
    print( "Happy birthday to you," )
    print( "Happy birthday, dear Stuart" )
    print( "Happy birthday to you," )

# printBar: prints a bar made of the character 'char',
# and with 'length' characters
def printBar( char, length ):
    print( char * length )
    

# printBarName: prints a bar made of the name of a person,
# with a variable length defined by 'length'.
# For example, printBar( "Dave", 10 ) will print the string
# "Dave*Dave*" (without the quotes).  This string has length
# 10.   printBar( "Dave",12 ) would print "Dave*Dave*Da".
def printBarName( name, length ):
    line = (name +"*" ) * length
    print( line[0:length]  )

# singHappy: sings happy birthday to a person whose name
# is passed in the parameter 'name'.  Prints a bar above
# and below the song.  The bar is the same length as the
# line where the person's name is mentioned.
def singHappy( name ):

    # create a string for the part of the line that contains
    # the person's name.
    line = "Happy birthday, dear"

    # print a bar that is the same length as the line
    # containing the person's name.
    #printBar( "=", len( line ) + len( name ) + 1 )
    printBarName( name, len( line ) + len( name ) + 1)

    # sing happy birthday
    print( "Happy birthday to you," )
    print( "Happy birthday to you," )
    print( line, name )  # prints "Happy birthday, dear xxxx"
                         # where xxxx is whatever is in name.
    print( "Happy birthday to you," )

    # print another bar
    #printBar( "=", len( line ) + len( name ) + 1 )
    printBarName( name, len( line ) + len( name ) + 1)

    # a couple blank lines...
    print( "\n\n" )

# createTextFile.  Utility function that I will use to
# quickly create a text file with some information in it.
# This function automatically stores the file in the same
# directory where this program is saved.
def createTextFile():
    file = open( "names.txt", "w" )
    file.write( "Gru Stuart Dave Carl Tim Jorge\n" )
    file.close()

# debugging function.  Just to test printBarName() on its
# own, without running the rest of the program.
def main0():
    printBarName( "Dave", 10 )


# MAIN FUNCTION
# This is where the program starts.
# The code sections between """ quotes are strings,
# and regarded by the interpreter as nothing more than
# a string.  We use this to comment out large sections of
# code.
def main():
    # create the text file with
    # minion names in it.  Comment this line once you have
    # run your program once (you don't want to keep on
    # recreating this file all the time!)
    createTextFile()

    """
    # sing happy birthdy to Dave and Stuart
    singHappyDave()
    print()
    singHappyStuart()
    print()
    """

    """
    # sing happy birthday to 2 people, but this
    # time we use the same function to perform the
    # same task, but pass the name of the person
    # via a parameter.
    singHappy( "Dave" )
    print()
    singHappy( "Stuart" )
    print()
    """


    # Sings happy birthday to a list of people whose
    # names are in a string, separated by spaces.
    """
    # list of people to sing to...
    minions = "Gru Stuart Dave Carl Tim Jorge"
    names = minions.split( ' ' )
    #print( names )
    for name in names:
        singHappy( name )
    """

    
    # Read names from a file called names.txt on disk,
    # and sing happy birthday to all the people listed
    # We assume that all the names are listed on one
    # line, all separated by spaces.
    file = open( "names.txt", "r" )
    line = file.read()
    file.close()

    # remove whitespace at beginning and end of string
    #print( line, "!" )  # debugging statement to check
                         # if there is a \n at the end
                         # of line.
    line = line.strip()  # we found out there was one,
                         # so we strip the line of
                         # leading and trailing whitespace
                         # (space, tab, and \n)
    
    names = line.split( ' ' )
    for name in names:
        singHappy( name )
        
    
# ----------------------------------------------------------
#                         M A I N 
# ----------------------------------------------------------
main()