Python代码转换中的返回函数问题

2024-04-26 06:03:10 发布

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

用英文(filename,character\u limit)编写一个函数文件,其中包含一个文件名(str)和一个字符\u limit(int)。文件名是要从拉丁代码转换为英语的文件名,字符限制是可以转换的最大字符数(包括换行符)。你知道吗

函数应返回一个字符串,该字符串包含与文件相同顺序的所有转换行

如果超出了限制(即,转换后的句子将使输出超过限制),则不应将字符计数超过限制的句子添加到输出中。应在输出的末尾添加一行带“<;>;”。然后应该停止对行的处理。你知道吗

文件中的每一行都是拉丁语代码的一个句子,你的程序应该打印出每个句子的英文版本

函数应该不断添加语句,直到文件中的输入用完或打印的字符总数(包括空格)超过限制为止。你知道吗

  • 你必须包含并调用你的英语句子功能。 不能在代码中的任何地方使用break语句。 您必须在文件中使用while\u in\u英语函数。你只能用一个 每个函数的返回语句。你知道吗

输入文本文件包含以下数据:

aughterleeoow anmeeoow essaymeeoow onmeeoow heteeoow eaningmeeoow ofmeeoow 
heteeoow omicceeoow ybeeoow enriheeoow ergsonbeeoow embermeeoow ofmeeoow  
heteeoow institutemeeoow rofessorpeeoow atmeeoow   
heteeoow ollegeceeoow edeeoow rancefeeoow authorisedmeeoow ranslationteeoow
ybeeoow loudesleyceeoow reretonbeeoow .leeoow esmeeoow .leeoow (paris),meeoow 
.a.meeoow (cantab)meeoow andmeeoow redfeeoow othwellreeoow .a.beeoow 
(london)meeoow ranslators'teeoow refacepeeoow histeeoow ork,weeoow ybeeoow 
rofessorpeeoow ergson,beeoow asheeoow eenbeeoow evisedreeoow inmeeoow 
etaildeeoow ybeeoow heteeoow authormeeoow imself,heeoow andmeeoow heteeoow 
resentpeeoow ranslationteeoow ismeeoow heteeoow onlymeeoow authorisedmeeoow 
one.meeoow orfeeoow histeeoow 

这是我的程序:(期望输出与我的输出不同)

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"""
space = ""
newone = open(filename)
nowline = newone.readline()  
characters = 0
while characters < character_limit and nowline != "":
    process = nowline[0:-1]
    space += english_sentence(process)+'\n'
    characters += len(nowline)
    nowline = newone.readline()
if characters > character_limit:
    space += "<<Output limit exceeded>>"

return space

Test Case:
ans = file_in_english('big_test.txt', 112)
print(ans)

> Obtained Output:

laughter
(man or an) (messay or essay) (mon or on) the (meaning or eaning) (mof or 
of) the comic by henri bergson
<<Output limit exceeded>>

Exected Output:
laughter
(man or an) (messay or essay) (mon or on) the (meaning or eaning) (mof or of)   the comic <<Output limit exceeded>>

Test Case 2: 

ans = file_in_english('big_test.txt', 8)
print(ans)

Obtained Output:
laughter
<<Output limit exceeded>>

EXPECTED Output:
<<Output limit exceeded>>

请告诉我哪里出错了。你知道吗


Tags: or文件函数inoutputenglish字符sentence
1条回答
网友
1楼 · 发布于 2024-04-26 06:03:10

在将当前翻译的句子附加到输出space之后,您将检查长度(存储在characters变量中)。在附加到输出之前,应检查长度是否超过限制:

def file_in_english(filename, character_limit):
    space = ""
    newone = open(filename)
    newline = english_sentence(newone.readline()) + '\n'
    while newline != '\n' and len(space) + len(newline) <= character_limit:
        space += newline
        newline = english_sentence(newone.readline()) + '\n'
    if len(space) + len(newline) > character_limit:
        space += "<<Output limit exceeded>>"
    return space

相关问题 更多 >