CSC111 Lab 9 Solution Programs 2014

From dftwiki3
Revision as of 06:27, 4 April 2014 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 07:25, 4 April 2014 (EDT)



Solution program for Lab #9.

# challenge 1
def writeTextFile( fileName, poemText ):
    file = open( fileName, 'w' )
    file.write( poemText )
    file.close()

def challenge1():
    text = """An Evening by Gwendolyn Brooks

A sunset's mounded cloud; 
A diamond evening-star; 
Sad blue hills afar; 
Love in his shroud. 

Scarcely a tear to shed; 
Hardly a word to say; 
The end of a summer day; 
Sweet Love dead."""
    writeTextFile( "poem.txt", text )

# challenge 2
def readTextFile( fileName ):
    file = open( fileName, "r" )
    text = ""
    for line in file:
        text = text + line
    file.close()
    return text

def challenge2():
    text = readTextFile( "poem.txt" )
    for line in text.split("\n" ):
        print( line )


# reading and writing numbers
def writeIntFile( fileName, list ):
    file = open( fileName, "w" )
    for x in list:
        file.write( "%d\n" % x )
    file.close()

def readIntFile( fileName ):
    file = open( fileName, "r" )
    list = []
    for line in file:
        line = line.strip()
        x = int( line )
        list.append( x )
    return list
    
def playingWithNumbers():
    numbers = [ 1, 2, 10, 100, 50, 7 ]
    # write the list to file, one number per line
    writeIntFile( "numbers.txt", numbers )

    # read the file of ints, and return it as a list of ints.
    newNumbers = readIntFile( "numbers.txt" )

    # print the list of ints and their sum (should be 170)
    for x in newNumbers:
        print( x )

    print( "Sum = ", sum( newNumbers ) )


#--- LOG ---
from random import choice
def discussion1():
     print( "\n\nDiscussion 1\n\n" )
     log = open( "log.txt", "w" )
     canned = [ "Please go on...", "Is that so?", "Please tell me more.", "Really?" ]
     print( "Hello!" )
     log.write( "Hello!\n" )
     while True:
           answer = input( "> " ).strip()
           log.write( "> " + answer + "\n" )
           if answer.lower() in [ "bye", "ciao", "later" ]:
                 break
           reply = choice( canned )
           print( reply )
           log.write( reply + "\n" )
           
     print( "Good bye!" )
     log.write( "Good bye!\n\n" )
     log.close()

def log( text ):
    file = open( "log.txt", "a" )
    file.write( text + "\n" )
    file.close()

def discussion2():
     print( "\n\nDiscussion 2\n\n" )
     canned = [ "Please go on...", "Is that so?", "Please tell me more.", "Really?" ]
     print( "Hello!" )
     log( "Hello!" )
     while True:
           answer = input( "> " ).strip()
           log( "> " + answer )
           if answer.lower() in [ "bye", "ciao", "later" ]:
                 break
           reply = choice( canned )
           print( reply )
           log( reply )
           
     print( "Good bye!" )
     log( "Good bye!\n" )

# Challenge of the Day
def createLab9Data():
    file = open( "lab9data.txt", "w" )
    file.write( """index name     cash   age   #-fans
1    Doc       5.50   101   100
2    Dopey     5.60    78   150
3    Bashful   6.00    99   500
4    Grumpy    4.80   100   99
5    Sneezy    7.00    NA   876
6    Sleepy    9.00    83   10
7    Happy     ---     85   1,000,001\n""" )
    file.close()

def catchingExceptions1( fileName ):
    file = open( fileName, "r" )

    L = []
    for line in file:
       words = line.split( )
       
       index = int( words[0] )
       name = words[1]
       cash = float( words[2] )
       age = int( words[3] )
       fans = int( words[4] )
       L.append( (name, cash, age, fans ) )

    print( "L = ", L )
               


    
def main():
    #challenge1()
    #challenge2()
    #playingWithNumbers()
    #discussion1()
    #discussion2()
    createLab9Data()
main()