Problem with Return function in Code Conversion Python
用英文(文件名,字符限制)写一个函数文件,文件名(str)和字符限制(int)。文件名是要从代码拉丁语转换为英语的文件名,字符限制是可以转换的最大字符数(包括换行符)。
函数应返回一个字符串,该字符串包含与文件顺序相同的所有转换行。
如果超过了限制(即,转换后的句子会使输出超过限制),则不应将字符计数超过限制的句子添加到输出中。应在输出的末尾添加一行"<>"。然后应停止处理行。
文件中的每一行都将是一个拉丁语代码的句子,您的程序应该打印出每个句子的英文版本。
函数应一直添加语句,直到文件中的输入用完或打印的字符总数(包括空格)超过限制。
- 你必须包含并调用你的英语句子功能。不能在代码中的任何地方使用break语句。在文件中必须使用"while"的"in"英文功能。你只能用一个每个函数返回语句。
输入文本文件包含以下数据:
1 2 3 4 5 6 7 8 9 10 11 | 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 |
这是我的程序:(期望输出与我的输出不同)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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)+' ' 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>> |
请告诉我哪里出错了。
您正在检查长度(存储在您的
1 2 3 4 5 6 7 8 9 10 11 12 13 | def file_in_english(filename, character_limit): space ="" newone = open(filename) newline = english_sentence(newone.readline()) + ' ' while newline != ' ' and len(space) + len(newline) <= character_limit: space += newline newline = english_sentence(newone.readline()) + ' ' if len(space) + len(newline) > character_limit: space +="<<Output limit exceeded>>" return space |