0

私はこのコードを持っています。ここで、次の数を数えようとしています:

  • .py スクリプトのコード行
  • for_loops ("for ") -while_loops ("while ")
  • if_statements ("if ")
  • 関数定義 (「def」)
  • 乗算記号 (「*」
  • 除算記号 (「/」
  • 追加記号 (「+」)
  • 減算記号 ("-")

数学記号ではコードは機能しますが、コードが if ステートメントを探しているときは 2 を返します。これが主な問題ですが、for ループを間違って記述したと思われるため、さらに多くの問題が発生する可能性があります。問題は後で。これに加えて、著者の名前の代わりに[]として表示される著者行を印刷する方法がわかりません

コード:

from collections import Counter
FOR_=0
WHILE_=0
IF_=0
DEF_=0
x =input("Enter file or directory: ")
print ("Enter file or directory: {0}".format(x))
print ("Filename {0:>20}".format(x))
b= open(x)
c=b.readlines()
d=b.readlines(2)
print ("Author {0:<18}".format(d))
print ("lines_of_code {0:>8}".format((len (c))))
counter = Counter(str(c))
for line in c:
    if  ("for ") in line:
        FOR_+=1
        print ("for_loops {0:>12}".format((FOR_)))
for line in c:
    if  ("while ") in line:
        WHILE_+=1
        print ("while_loops {0:>10}".format((WHILE_)))
for line in c:
    if  ("if ") in line:
        IF_+=1
        a=IF_
        print ("if_statements {0:>8}".format((a)))
for line in c:
    if  ("def ") in line:
        DEF_+=1
        print ("function_definitions {0}".format((DEF_)))
print ("multiplications {0:>6}".format((counter['*'])))
print ("divisions {0:>12}".format((counter['/'])))
print ("additions {0:>12}".format((counter['+'])))
print ("subtractions {0:>9}".format((counter['-'])))

読み取り元のファイル:

'''Dumbo
Author: Hector McTavish'''
    for for for  # Should count as 1 for statement
while_im_alive # Shouldn't count as a while
while blah # But this one should
  if defined # Should be an if but not a def
  def if # Should be a def but not an if
    x = (2 * 3) + 4 * 2 * 7 / 1 - 2  # Various operators

どんな助けでも大歓迎です

4

2 に答える 2