
本文共 1378 字,大约阅读时间需要 4 分钟。
我目前正在做一个程序,我已经分配了,这是刚刚得到的字数和行数的十四行诗。这里的第一段代码可以工作,并且是我的教授想要的正确输出,即使它包含了十四行诗的前两行。在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.
转载地址:https://blog.csdn.net/weixin_34759094/article/details/113503056 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
