2017-09-28 6 views
-4

変数のパーセンテージを取得する方法がわかりません。ここでは、正確な割り当てがあります:Python 3で変数のパーセンテージを取得

は10以上のものを持っている を、テキストファイルの各行 に保存されている単語のリストを、というPythonでプログラムを作成し、プリントし、長い単語の数をカウントし、すなわち言葉テキストファイル内の単語の総数gが与えられた場合に、単語の長さの割合が、 ワードであることを示します。

そして、ここに私のコードです:

wordcount = 0 
longwordcount = 0 

for line in sys.stdin: 
    line = line.rstrip() 
    charnum = len(line) 
    wordcount = wordcount + 1 
    percentage = int(longwordcount // wordcount) * 100 
    if charnum > 10: 
     longwordcount = longwordcount + 1 
     print(line) 



print("There are {0} words in the word list that have more than 10 characters.\nThis is {1}% of the total number of words in the text file." 
.format(longwordcount, percentage)) 
+0

ワード数を数えます。 * long *単語の数を数えます(すべて同じループ内にあります)。 **最後に**、あなたがすべてを数えたら、パーセンテージを計算してください。 – deceze

+0

まず、 'sys.stdin'は理想的ではありません。パスを定義したり、 'sys.argv [1]'を使ったり、 'with open()as ... 'を使ってopen/readしたりする方が良いでしょう。 – pstatix

+0

'int(longwordcount // wordcount)* 100'は0回100回、または1回100回です。おそらく' longwordcount * 100 // wordcount'を意味します。そして、最後にそれを計算してください。 – khelwood

答えて

0

代わりに床除算演算子ではなくint型変換の除算演算子を使用します。必要に応じて、format文を使用して桁数を制限することができます。

パーセント=ロングワードカウント/ワードカウント* 100

関連する問題