如何在pythonshell中一次打印一个单词?

2024-04-26 05:36:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经有了执行此任务的代码,但只能在命令提示符下执行。输入到函数中的数字是每分钟单词数,因此如果输入60个单词,将得到每分钟60个单词,或每秒一个单词。在

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)

我被告知我必须导入诅咒才能在shell中工作。我该怎么做?我需要写一些完全不同的东西吗?在


Tags: importdatalentimeiswithstdoutsys
1条回答
网友
1楼 · 发布于 2024-04-26 05:36:44

我不清楚问题是什么,但只要将\r\n改为\n并消除填充,可能会给您一些有用的东西,而不必诅咒:

#!/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)

相关问题 更多 >