fin = open("dictionary.txt", "r") # open file for reading fout = open("extracredit.txt", "w") # open file to save list of DOLLAR words, # for extra credit opportunity! #For every line, get each word for original in fin: # get next line of fin, the input file... ... #Write dollar words to file if cost==100: fout.write(original + "\n") # write to the output file print("Output file written called 'extracredit.txt'") fin.close() # close the files... fout.close()
Why do we subtract 96 from the ordinal or ASCII value of the char sometimes but subtract 64 at other times? Does either 96 or 64 get subtracted from the ord() of every char? ----- 65 - 64 is ?? 97 - 96 is ?? 90 - 64 is ?? 122 - 86 is ??
windsor:~/web/1510/files> wc dictionary.txt <- wc is Unix word count command. 80368 80368 622783 dictionary.txt 80368 lines 80368 words (so must be one word per line in the file) 622783 622,783 bytes or characters in the file dictionary.txt Question to think about and answer after watching the video and studying the programs: 622783 / 80368 7.74914144933306788771 622783 -------- = 7.74914144933306788771 80368 Is the average word length of words in dictionary.txt = 7.75
Compare it to the original version available to Sarah's class a day or two before Friday's class. They should be pretty similar, but any different approach that you notice and understand will only increase your fluency with Python and programming in general!
Try it out. First download the dictionary.txt file and save it in the
same directory where you will save and run your dollarWords.py program.
Look at the extracredit.txt file to see the output when you are done
with the run of dollarWords.py. The file extracredit.txt will be
just those words from dictionary.txt that are DOLLAR words!
Output file written called 'extracredit.txt' There were 1018 dollar words out of 80368 dictionary words. >>> ---- -----
The more developed version with additional features: dollarWords3.py ...