Problem Statement

Write a program that accepts three files as command line arguments. The first two represent input files, and the third one represents the desired output file. If there aren't three arguments provided, either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program. The output file may be an existing file, since it will be overwritten.

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit. As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

Whaaaa?

Image Credit: Mashable via Giphy

Let's break it down into smaller parts

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

Write a program that accepts three files as command line arguments.

If there aren't three arguments provided, ... print "Invalid Arguments" and exit the program.

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit()

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit()

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit()

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

<mark># -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-</mark>

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit()

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program


The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

for line in scanner1:
  line = line.strip()

for line in scanner2: line = line.strip()

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

for line in scanner1:
  line = line.strip()
  input = int(line)

for line in scanner2: line = line.strip() input = int(line)

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

for line in scanner1:
  line = line.strip()
  input = int(line)

for line in scanner2: line = line.strip() input = int(line)

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit()

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit() except ValueError as e: print(“Invalid Input”) sys.exit()

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.

for line in scanner1:
  line = line.strip()
  input = int(line)
  if input % 2 == 0:
    # even
  else:
    # odd

for line in scanner2: line = line.strip() input = int(line) if input % 2 == 0: # even else: # odd

As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.

countEven = 0
countOdd = 0
sumEven = 0
sumOdd = 0

for line in scanner1: line = line.strip() input = int(line) if input % 2 == 0: countEven += 1 sumEven += input else: countOdd += 1 sumOdd += input

for line in scanner2: line = line.strip() input = int(line) if input % 2 == 0: countEven += 1 sumEven += input else: countOdd += 1 sumOdd += input

As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.

countEven = 0
countOdd = 0
sumEven = 0
sumOdd = 0

for line in scanner1: line = line.strip() input = int(line) if input % 2 == 0: countEven += 1 sumEven += input else: countOdd += 1 sumOdd += input

for line in scanner2: line = line.strip() input = int(line) if input % 2 == 0: countEven += 1 sumEven += input else: countOdd += 1 sumOdd += input

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.

writer.write("{}\n".format(countEven))
writer.write("{}\n".format(sumEven))
writer.write("{}\n".format(countOdd))
writer.write("{}\n".format(sumOdd))

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.

writer.write("{}\n".format(countEven))
writer.write("{}\n".format(sumEven))
writer.write("{}\n".format(countOdd))
writer.write("{}\n".format(sumOdd))

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

writer.write("{}\n".format(countEven))
writer.write("{}\n".format(sumEven))
writer.write("{}\n".format(countOdd))
writer.write("{}\n".format(sumOdd))

print(“Complete”)

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

writer.write("{}\n".format(countEven))
writer.write("{}\n".format(sumEven))
writer.write("{}\n".format(countOdd))
writer.write("{}\n".format(sumOdd))

print(“Complete”)

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

import sys

if len(sys.argv) != 4: print(“Invalid Arguments”) sys.exit()

try: with open(sys.argv[1]) as scanner1,
open(sys.argv[2]) as scanner2,
open(sys.argv[3], “w”) as writer:

# -=-=-=-=- MORE CODE GOES HERE -=-=-=-=-

except FileNotFoundError as e: print(“Invalid Arguments”) sys.exit() except IOError as e: print(“Invalid Arguments”) sys.exit() except ValueError as e: print(“Invalid Input”) sys.exit()

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

Already taken care of using
With Statement

"/js/highlight.pack.js"