Difference between revisions of "CSC111 Lab 9 Solution Programs 2014"
(Created page with "--~~~~ ---- <source lang="python"> # challenge 1 def writeTextFile( fileName, poemText ): file = open( fileName, 'w' ) file.write( poemText ) file.close() def cha...") |
|||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 07:25, 4 April 2014 (EDT) | --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 07:25, 4 April 2014 (EDT) | ||
---- | ---- | ||
+ | |||
+ | <br /> | ||
+ | Solution program for [[CSC111 Lab 9 2014| Lab #9]]. | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
# challenge 1 | # challenge 1 | ||
Line 152: | Line 156: | ||
main() | main() | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Labs]] |
Revision as of 06:27, 4 April 2014
--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()