How do I print one word at a time in python shell?

426 views Asked by At

I already have code for performing this task, but I can only perform this in the command prompt. The number you put into the function is words per minute, so if you input 60, you will get 60 wpm, or one word per second.

import sys
import time

data = "is sentence with some words this is a some. words this is a sentence with some words".split()


def inputwordsperminute(x):


    max_len=max([len(w) for w in data])
    pad = " "*max_len
    for w in data:
        sys.stdout.write('%s\r' % pad)
        sys.stdout.write("%s\r" % w)
        sys.stdout.flush()
        time.sleep((60.0/x))

print

inputwordsperminute(200)

I was told that I have to import curses in order to get this working in shell. How do I do that? Will I need to write something completely different?

1

There are 1 answers

2
dstromberg On

I"m not sure I'm clear on what the question is, but just changing your \r's to \n's and eliminating the padding may give something usable without curses:

#!/usr/local/cpython-2.7/bin/python

import sys
import time

data = "is sentence with some words this is a some. words this is a sentence with some words".split()

def inputwordsperminute(x):
    #max_len=max([len(w) for w in data])
    #pad = " "*max_len
    for w in data:
        #sys.stdout.write('%s\n' % pad)
        sys.stdout.write("%s\n" % w)
        sys.stdout.flush()
        time.sleep((60.0/x))

print

inputwordsperminute(200)