Solutions for Question
1 and Extension Task
Python Command-Line Arguments and Module Path
Date: May 20, 2025
Batch 7
[Link] (242FA05029)
[Link] VENU REDDY (242FA05024)
MANOJ BHUDANIYA (242FA05042)
PANDAY THUFAN (242FA05051)
Understanding [Link]
What is [Link]? Argument Indexing
A list holding command-line arguments passed [Link][0] is the script name. Subsequent indexes
to a Python script. hold inputs.
Example Usage :
import sys
print("Script name:", [Link][0]) output
print("Number of arguments:", len([Link])) Script name: [Link]
print("Arguments:", [Link][1:]) Number of arguments: 3
Arguments: ['hello', '123']
Run from terminal:
python [Link] hello 123
1(b). Program to add two values using [Link] with error handling
Python Script:
Save this as add_args.py:
import sys
try:
TO RUN IN TERMINAL
if len([Link]) < 3: python add_args.py 10 20
raise ValueError("Two numbers must be provided.")
num1 = float([Link][1]) OUTPUT :
num2 = float([Link][2]) Sum: 30.0
print("Sum:", num1 + num2)
except ValueError as ve:
If arguments are missing:
print("ValueError:", ve) ValueError: Two numbers must be provided.
except IndexError:
print("Error: Not enough command-line arguments.")
except Exception as e:
print("Unexpected error:", e)
1(c): Effect of [Link] on Module Importing
What is [Link]?
•[Link] is a list of strings that specifies the search path for Python modules.
•It is used by the interpreter to locate the modules during import.
Modifying [Link]:
You can manually add custom directories to [Link] to import modules from non-standard locations .
import sys
[Link]('/home/user/my_modules') # Add custom path
import mymath # Assuming [Link] is in the specified folder
[Link]()
Impact on Maintainability:
✅ Pros:
•Enables importing modules from custom or external directories.
•Useful in development environments.
❌ Cons:
•Can lead to ambiguous imports and debugging issues.
•Not recommended for large-scale applications.
•Better alternatives include virtual environments or dependency managers like pip.
Extension Task
A: Enhanced Command-line Script With Flags
Objective:
Use [Link] to accept multiple numeric arguments and perform operations like sum, average, or max based on a flag .
import sys
def show_help():
print("Usage: python [Link] --sum|--avg|--max num1 num2 ...")
print("Example: python [Link] --avg 10 20 30") EXAMPLE RUN :
python [Link] --max 3 15 9
try:
if len([Link]) < 3:
raise ValueError("Not enough arguments.")
flag = [Link][1]
numbers = list(map(float, [Link][2:]))
if flag == "--sum": OUTPUT
print("Sum =", sum(numbers))
elif flag == "--avg": Maximum = 15.0
print("Average =", sum(numbers) / len(numbers))
elif flag == "--max":
print("Maximum =", max(numbers))
elif flag == "--help":
show_help()
else:
raise ValueError("Unknown flag: " + flag)
except ValueError as ve:
print("Error:", ve)
except Exception as e:
print("Unexpected error:", e)E
b: Help and Error Handling
Provide a --help flag and implement structured exception handling for:
Invalid flags
Missing values
Wrong data types
Example Error Scenarios:
1) 3)
python [Link] --sum abc python [Link] --help
OUTPUT:
Error: could not convert string to float: 'abc' OUTPUT:
Usage: python [Link] --sum|--avg|--max num1 num2 ...
2)
python [Link]
OUTPUT
Error: Not enough arguments.