csci 4 :: programming project 2 (due by midnight on friday, oct 11)
Submitting your work:
Complete this exercise on your own.
Solve the problem described below by designing, implementing, and testing a
Email your Python module code to the instructor at firstname.lastname@example.org before the deadline.
The purpose of this project is to give you an opportunity to practice the Python programming skills you have learned and will learn in the coming weeks.
For this project, you will implement a program that tests a series of numbers to see
which ones are primes, and identify the smallest and largest primes encountered in the
series. The series of numbers will be loaded from a text file. The numbers will all
be in the range 2 to 9999.
Your program should function as follows:
- prompt the user for a file name, read and store the input file name
open the file (display an error message if the file does not exist) and read
one line at a time
ERROR: file3.txt cannot be opened for processing
each line of the file will have an integer -- test the integer to see if
it is prime (evenly divisible only by 1 and itself)
if the number is prime, display the number and the word "PRIME"
if the number is not prime, display the number and the word "NOT PRIME"
21: not prime
identify the smallest and largest primes identified in the series, and display
Largest prime: 19
Smallest prime: 3
maintain counters of the primes and non-primes encountered in the file, and
display these totals, and the file name, after all numbers in the file have
5 primes, 15 non-primes in file1.txt
- close the file
use a loop to allow the user to repeat the program and select a new file of
numbers to test
Document your Python module using this sample as a guide.
You can find sample input files for this program here.
The starter file here contains a function that you can
use to clear the screen, to improve the quality of your program output. We will talk about
how to use this function in class.
- practice using variables and arithmetic
- practice using decisions
- read program data from a text file
- use loops to develop algorithms and control program flow
- use try-except statements to handle program errors
- use the format function to produce formatted program output
- technical requirements:
[50 points] your module must be documented correctly (see
the basic_module example)
- [50 points] your program must compile and run without errors
- feature requirements:
[50 points] prompt a user for a file name, store the file name in a
variable, and use the file name to open a text file for processing
[50 points] use a try-except statement and display an error message
if the user inputs a file name for a non-existent file
[100 points] use the appropriate arithmetic operator to process a number
to see if it is a prime or non-prime number; display the result
[100 points] count the number of primes and non-primes identified in a
series of numbers; display the result
[100 points] identify the smallest and largest primes encountered in the series;
display the results
[100 points] use the format function to format program output; all
outputs should be neatly and consistently aligned; format program output
neatly using empty lines to space out program output as appropriate
If you have time, enhance your program to include one or more of the following features:
[25 points] display all of the factors (numbers that evenly divide into the number
being tested) of each number
12: 1 2 3 4 6 12
12: NOT PRIME
17: 1 17
17: not prime
[25 points] use a list to store all of the prime numbers identified during
the program run, and display this list (one list for all of the files the user
chooses to test) at the end of the program run
PRIMES IDENTIFIED: 2 7 11 13 19 23 1693
Total points for this programming project: