将函数返回与非

2024-09-30 19:27:46 发布

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

用英文(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”?你知道吗


Tags: 文件函数inreturnenglish字符sentence句子
2条回答

您必须确保,任何应该返回某些内容的函数都会以各种方式执行此操作,以结束函数。你知道吗

函数file_in_english只返回if characters > character_limit:情况下的某些内容

如果charachter ==charachter < character_limit不是这种情况,则函数不显式返回任何内容。你知道吗

任何函数如果不从它返回某些内容,则在它返回到调用方时隐式返回None。你知道吗

def something(boolean):
    """Function that only returns something meaninfull if boolean is True."""
    if boolean:
        return "Wow" 

print(something(True))  # returns Wow
print(something(False)) # implicitly returns/prints None

您可以在python教程中找到这一事实:

Coming from other languages, you might object that fib is not a function but a procedure since it doesn’t return a value. In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name). Writing the value None is normally suppressed by the interpreter if it would be the only value written. You can see it if you really want to using print():

来源:https://docs.python.org/3.7/tutorial/controlflow.html#defining-functions-就在第二个绿色示例框之后

你做的事情和:

def f():
    print('hello')
print(f())

所以基本上缩小到:

print(print('hello world'))

顺便说一句:

>>> type(print('hello'))
hello
<class 'NoneType'>
>>> 

要解决代码问题,请执行以下操作:

def file_in_english(filename, character_limit):
    s=""
    """English File"""
    newone = open(filename)
    nowline = newone.readline()  
    characters = 0
    while characters < character_limit and nowline != "":
        process = nowline[0:-1]
        s+=english_sentence(process)+'\n'
        characters += len(nowline)
        nowline = newone.readline()
    if characters > character_limit:
        s+="<<Output limit exceeded>>"

    return s


ans = file_in_english('test1.txt', 20)
print(ans)

相关问题 更多 >