[Tutor] commands

Dave Angel davea at ieee.org
Mon Mar 29 04:01:36 CEST 2010


Shurui Liu (Aaron Liu) wrote:
> # Translate wrong British words
>
> #Create an empty file
> print "\nReading characters from the file."
> raw_input("Press enter then we can move on:")
> text_file = open("storyBrit.txt", "r+")
> whole_thing = text_file.read()
> print whole_thing
> raw_input("Press enter then we can move on:")
> print "\nWe are gonna find out the wrong British words."
> corrections = {'colour':'color', 'analyse':'analyze',
> 'memorise':'memorize', 'centre':'center', 'recognise':'recognize',
> 'honour':'honor'}
> texto = whole_thing
> for a in corrections:
>     texto = texto.replace(a, corrections[a])
> print texto
>
> # Press enter and change the wrong words
> if "colour" in whole_thing:
>     print "The wrong word is 'colour' and the right word is 'color'"
> if "analyse" in whole_thing:
>     print "the wrong word is 'analyse' and the right word is 'analyze'"
> if "memorise" in whole_thing:
>     print "the wrong word is 'memorise' and the right word is 'memorize'"
> if "centre" in whole_thing:
>     print "the wrong word is 'centre' and the right word is 'center'"
> if "recognise" in whole_thing:
>     print "the wrong word is 'recognise' and the right word is 'recognize'"
> if "honour" in whole_thing:
>     print "the wrong word is 'honour' and the right word is 'honor'"
>
> # We are gonna save the right answer to storyAmer.txt
> w = open('storyAmer.txt', 'w')
> w.write('I am really glad that I took CSET 1100.')
> w.write('\n')
> w.write('We get to analyse all sorts of real-world problems.\n')
> w.write('\n')
> w.write('We also have to memorize some programming language syntax.')
> w.write('\n')
> w.write('But, the center of our focus is game programming and it is fun.')
> w.write('\n')
> w.write('Our instructor adds color to his lectures that make them interesting.')
> w.write('\n')
> w.write('It is an honor to be part of this class!')
> w = open("assign19/storyAmer.txt", "w")
>
> w.close()
>
>
>
> This is what I have done, I don't understand why this program cannot
> fix "analyse".
>
>   
You do some work in texto, and never write it to the output file.  
Instead you write stuff you hard-coded in literal strings in your program.

And you never fixed the mode field of the first open() function, as 
someone hinted at you.   And you don't specify the output file location, 
but just assume it's in the current directory.  For that matter, your 
open of the input file assumes it's in the current directory as well.  
But your assignment specified where both files would/should be.


DaveA



More information about the Tutor mailing list