Answer :

Answer:

Here's a Python program that uses a for loop with the range function and accepts a single argument to determine the number of iterations:

pythonCopy code1import sysdef main(num_iterations): for i in range(num_iterations): print(f"Iteration {i+1}")if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python script.py <num_iterations>") sys.exit(1) try: num_iterations = int(sys.argv[1]) if num_iterations <= 0: raise ValueError except ValueError: print("Invalid input. Please provide a positive integer.") sys.exit(1) main(num_iterations)  ההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Explanation:

We import the sys module to access command-line arguments.

We define a main function that takes a single argument num_iterations. This function contains the for loop that iterates num_iterations times using the range function. Inside the loop, we print the current iteration number.

In the if __name__ == "__main__": block, we check if the script is being run directly (not imported as a module).

We check if the number of command-line arguments is not equal to 2 (the script name and the number of iterations). If the number of arguments is incorrect, we print a usage message and exit the script with a status code of 1.

We use a try-except block to handle potential errors when converting the command-line argument to an integer. If the conversion fails or the provided value is not a positive integer, we print an error message and exit the script with a status code of 1.

If the input is valid, we call the main function with the provided number of iterations.

To run the script, open a terminal or command prompt, navigate to the directory where the script is saved, and run the following command:

python script.py <num_iterations>

Replace <num_iterations> with the desired number of iterations.

For example, to run the script with 5 iterations, you would run:

python script.py 5

The output will be:

Iteration 1

Iteration 2

Iteration 3

Iteration 4

Iteration 5

This program demonstrates how to use a for loop with the range function and accept a single command-line argument to determine the number of iterations.