python中计数器函数_Python中使用多个函数的字计数器

我目前正在做一个程序,我已经分配了,这是刚刚得到的字数和行数的十四行诗。这里的第一段代码可以工作,并且是我的教授想要的正确输出,即使它包含了十四行诗的前两行。在import string

def main():

ifName = input("What file would you like to analyze? ")

ofName = input("What file should the results be written to? ")

infile = open(ifName, "r")

outfile = open(ofName, "w")

lineCount = 0

wordCount = 0

for line in infile:

lineCount +=1

wordLine = line.split()

L = len(wordLine)

wordCount += L

print("The file", ifName, "had:", file= outfile)

print("words =", wordCount, file= outfile)

print("lines =", lineCount, file= outfile)

print("The results have been printed to:", outfile)

infile.close

outfile.close

main()

然而,赋值的下一部分是使用第二个函数“countNum”和参数“line”获得相同的结果。所以countNum(行)。在

这是我一直在纠结的代码,看看能不能让它正常工作。在

^{pr2}$

如果你想知道,这是十四行诗.txt文件:Shakespeare’s Sonnet 18

Shall I compare thee to a summer's day?

Thou art more lovely and more temperate:

Rough winds do shake the darling buds of May,

And summer's lease hath all too short a date:

Sometime too hot the eye of heaven shines,

And often is his gold complexion dimm'd;

And every fair from fair sometime declines,

By chance or nature's changing course untrimm'd;

But thy eternal summer shall not fade

Nor lose possession of that fair thou owest;

Nor shall Death brag thou wander'st in his shade,

When in eternal lines to time thou growest:

So long as men can breathe or eyes can see,

So long lives this, and this gives life to thee.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值