November 17th, 2013, 06:45 PM
Analyzing a file with read function?
I am working on an assignment and am a bit stuck. Here is the problem:
Write code that takes any of the script you have written during this class and analyzes the contents of the script and output the number of:
Lines of codes
Counting the number of lines is easy, but I have no idea how to check for specific statements and such? Your help would be appreciated. Here is what I have so far:
c = open("nested_loop3.py", "r")
line_count = 0
for line in c:
line_count += 1
November 17th, 2013, 07:18 PM
I think you will find the in operator to be your best friend here.
To find prints for example:
Of course if you want the program to be perfect you would have to make sure that the occurence was not in the context of a comment or string, but I somehow doubt your instructor is wanting that level of sophistication.
if "print" in line:
print_count += 1
Last edited by Mekire; November 17th, 2013 at 07:20 PM.
November 17th, 2013, 08:13 PM
I forgot about the in operator. Of course! Thank you Mek.
Originally Posted by Mekire