2017-09-30 2 views
-5

''()を使わずに答えを返すにはどうすればよいですか?余分な ''と()を使わずに答えを返すにはどうすればいいですか

def analyze_text(text): 
    #removes all of the non-al charaters 
    new_string = "" 
    for eachLetter in text: 
     if eachLetter.isalpha(): 
      new_string += eachLetter 
    #count the number of e 
    text_count_E = text.count("E") 
    text_count_e = text.count("e") 
    total_e = text_count_E + text_count_e 
    #gets the percentage of e 
    percentage = 100 * total_e/len(new_string) 

    return "The text contains ",len(new_string),"alphabetic characters, of 
    which",total_e,"(",percentage,")","are 'e'" 

答えて

0

、その後、

return len(new_string), total_e 

そして、関数の外で、あなたの出力を印刷し、必要な値を返してみタプルではなく、単一の文字列を返しPlsは、次回より読みやすいコードリストを提供します。

各文字列オブジェクトが実行できるformat()メソッドを使用できます。 これは参考になるかもしれません。あなたのケースでは https://pyformat.info/#simple

このようなものになります。

return "The text contains {} alphabetic characters, of which {} ({}) are 'e'".format(len(new_string), total_e, percentage) 
+2

あなたの答えにリストより読みやすいコードを入力してください。 – ekhumoro

0

あなたは

だけ

new_len, total_e = analyze_text(text) 
percent = 100 * total_e/new_len 

print("The text contains {} alphabetic characters, of which {} ({}) are 'e'".format(new_len, total_e, percent)) 
関連する問題