wordcount.py


Below is the syntax highlighted version of wordcount.py from §1.5 Input and Output.


#-----------------------------------------------------------------------
# wordcount.py
#-----------------------------------------------------------------------

import stdio

# Read a sequence of strings from standard input, and write the number
# of strings to standard output.

count = 0

while not stdio.isEmpty():
    word = stdio.readString()
    count += 1

stdio.writeln('number of words  = ' + str(count))

#-----------------------------------------------------------------------

# python wordcount.py < tale.txt 
# number of words  = 139043


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Wed Jun 26 23:38:37 EDT 2019.