Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: Lines One way to measure the complexity of a program is to count its number of lines of codeLinks to an external site. (

Python: Lines
One way to measure the complexity of a program is to count its number of lines of codeLinks to an external site. (LOC), excluding blank lines and comments. For instance, a program like
# Say hello
name = input("What's your name? ")
print(f"hello,{name}")
has just two lines of code, not four, since its first line is a comment, and its second line is blank (i.e., just whitespace). Thats not that many, so odds are the program isnt that complex. Of course, just because a program (or even function) has more lines of code than another doesnt necessarily mean its more complex. For instance, a function like
def is_even(n):
if n %2==0:
return True
else:
return False
isnt really twice as complex as a function like
def is_even(n):
return n %2==0
even though the former has (more than) twice as many lines of code. In fact, the former might arguably be simpler if its easier to read! So lines of code should be taken with a grain of saltLinks to an external site..
Even so, implement a program that counts the lines of a file, and outputs the number of lines of code in that file, excluding comments and blank lines. If the user does not specify exactly one command-line argument, or if the specified files name does not end in .py, or if the specified file does not exist, the program should instead exit via sys.exit.
Assume that any line that starts with #, optionally preceded by whitespace, is a comment. (A docstringLinks to an external site. should not be considered a comment.) Assume that any line that only contains whitespace is blank.
Additionally, your program must include:
comments with your name, date, program name, and program purpose.
a main function with if __name__=='__main__': code. See this tutorial from module 5.
Hints
How to Test
Heres how to test your code manually:
Run your program with python LastNameFirstInitial_Lines.py. Your program should exit with sys.exit and provide an error message:
Too few command-line arguments
Create two python programs, hello.py and goodbye.py. Run python LastNameFirstInitial_Lines.py hello.py goodbye.py. Your program should exit with sys.exit and provide an error message:
Too many command-line arguments
Create a text file called invalid_extension.txt. Run your program with python LastNameFirstInitial_Lines.py invalid_extension.txt. Your program should exit with sys.exit and provide an error message:
Not a Python file
Run your program with python LastNameFirstInitial_Lines.py non_existent_file.py. Assuming non_existent_file.py doesnt exist, your program should exit with sys.exit and provide an error message:
File does not exist
Create additional python programs which vary in complexity: create some with comments, some docstrings, and some whitespace. For each of these files run python LastNameFirstInitial_Lines.py FILENAME where FILENAME is the name of the file. Lines.py should output the number of lines, excluding comments and whitespace, present in the given file.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions

Question

2. What would you do first if you were Jennifer?

Answered: 1 week ago

Question

State the uses of job description.

Answered: 1 week ago

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago