用英文(filename,character\u limit)编写一个函数文件,其中包含一个文件名(str)和一个字符\u limit(int)。文件名是要从Cat拉丁语转换为英语的文件名,字符限制是可以转换的最大字符数。限制为输出中的字符总数(包括换行符)。你知道吗
函数应该返回一个字符串,该字符串包含与文件相同顺序的所有转换行—记住每行末尾的换行符(即确保在每个转换行的末尾包含一个换行符,以便它包含在行的长度中)。你知道吗
如果超出了限制(即,转换后的句子将使输出超过限制),则不应将字符计数超过限制的句子添加到输出中。应在输出的末尾添加一行带“<;>;”。然后应该停止对行的处理。你知道吗
文件中的每一行都是一个奇怪的拉丁语句子,你的程序应该打印出每个句子的英文版本
函数应该不断添加语句,直到文件的输入用完或打印的字符总数(包括空格)超过限制为止。你知道吗
答案必须包括你对英语句子的定义和它的辅助功能-我应该称之为英语单词或类似的。你知道吗
您必须在文件中使用while\u in\u英语函数。你知道吗
You can only use one return statement per function.
示例中使用的测试文件(test1.txt)具有以下数据:
impleseeoow estteeoow aseceeoow
impleseeoow estteeoow aseceeoow ineleeoow 2meeoow
impleseeoow estteeoow aseceeoow ineleeoow 3meeoow
impleseeoow estteeoow aseceeoow ineleeoow 4meeoow
我认为这个程序运行得很好,只是有时它不返回任何结果。你知道吗
def english_sentence(sentence):
"""Reverse Translation"""
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
eng_sentence = []
for coded_word in sentence.split():
if coded_word.endswith("eeoow") and (coded_word[-6] in consonants):
english_word = coded_word[-6] + coded_word[:-6]
if (coded_word[-6] == 'm') and (coded_word[0] not in consonants):
english_word = '(' + english_word + ' or ' + coded_word[:-6] + ')'
eng_sentence.append(english_word)
return " ".join(eng_sentence)
def file_in_english(filename, character_limit):
"""English File"""
newone = open(filename)
nowline = newone.readline()
characters = 0
while characters < character_limit and nowline != "":
process = nowline[0:-1]
print(english_sentence(process))
characters += len(nowline)
nowline = newone.readline()
if characters > character_limit:
return("<<Output limit exceeded>>")
ans = file_in_english('test1.txt', 20)
print(ans)
输出为:
simple test case
simple test case line (m2 or 2)
simple test case line (m3 or 3)
simple test case line (m4 or 4)
None
但我必须在每个函数中只使用一个return语句。如何对第二个函数执行此操作并避免输出中的“None”?你知道吗
您必须确保,任何应该返回某些内容的函数都会以各种方式执行此操作,以结束函数。你知道吗
函数
file_in_english
只返回if characters > character_limit:
情况下的某些内容如果
charachter ==
或charachter < character_limit
不是这种情况,则函数不显式返回任何内容。你知道吗任何函数如果不从它返回某些内容,则在它返回到调用方时隐式返回
None
。你知道吗您可以在python教程中找到这一事实:
来源:https://docs.python.org/3.7/tutorial/controlflow.html#defining-functions-就在第二个绿色示例框之后
你做的事情和:
所以基本上缩小到:
顺便说一句:
要解决代码问题,请执行以下操作:
相关问题 更多 >
编程相关推荐