Loops && Booleans

Your Assignment

The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture.

You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number.

Assignment Requirements

  1. Three functions are required:

    • get_input():

    This function takes no parameters, but will ask the user for an integer number. It will return a valid integer. You will use an indefinite loop and exception handling like we discussed in class. It is required to catch user input error and gracefully recover. Learn about the continue keyword and how it works in a loop, it can be handy.

    • is_prime(n):

      This function will take an integer parameter n, and return True if the number is prime, or return False if it is not prime. You can use this pseudocode for primality testing to write this function.

    • main():

      This does the bulk of your program’s work for solving Goldbach’s conjecture. You will call get_input() and is_prime(n) from main().

      Goldbach’s conjecture itself will be solved using an indefinite loop. Particularly a ‘post-test’ loop pattern. A for loop will not be accepted as part of the solution.

Sample output

A successful run:

This program tests the Goldbach's conjecture
Please enter an even integer larger than 2: 36
36 = 5 + 31

A run recovering from errors:

This program tests the Goldbach's conjecture
Please enter an even integer larger than 2: 7
Wrong input!
Please enter an even integer larger than 2: foo
Bad input!
Please enter an even integer larger than 2: 8
8 = 3 + 5

If by chance the number chosen is valid, but doesn’t hold for Goldbach’s conjecture, then print:

Goldbach's conjecture doesn't hold for [whatever the number is]

(I’m not aware of a number for which this occurs, but handle this possible case anyways.)

Submission Requirements

There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Submit a file named lab_loops_booleans.py.

  2. Documentation String (“docstring”): The following docstring (properly edited with your name) needs to be the very first thing in your Python program file.

    """CSC 161 Lab: Loops && Booleans
    This lab...
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015

    Read more about docstrings: Program Documentation String

  3. Comments (lines prefaced with #): Unclear parts of your code can receive point deductions, so be judicious about using comments. Do not overuse comments. Too much unnecessary commenting can complicate reading otherwise clear code!

    Read more about comments: Program Comments

  4. Python Code Style: You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

  5. The “__main__, top-level script environment” check is required:

    if __name__ == '__main__':

    You can read more about this trick on the official Python website.

The Blackboard post for this lab assignment will contain the details regarding the due date and other details regarding class administration for this assignment.


The following is the grading rubric for this lab to be used by the course graders. The lab is worth 100 points.

Reasonable variations of this lab should be acceptable. In particular, we would accept programs that take input files with different formats (comma separated, space separated, one number per line, …)

Here’s a brief grading rubric:

  • Syntax errors: -30 points
  • A bad/missing docstring (i.e. no name, lab section, etc): -10 points
  • The submitted filename is wrong: -5 points
  • No Python comments used when they are needed to clarify code: -5 points
  • Code doesn’t attempt to adhere to Python coding style: -10 points
  • Your top-level script environment check is missing: -5 points
  • Missing main() function: -10 points
  • Three functions are required: get_input(), is_prime(n) and main: -10 each/missing
  • The code for get_input() and main() is to be written using a while loop, only: -15 points
  • Boolean logic errors for if and while conditions: -5 points each

There may be additional criteria for grading, this is just a summary of the major rubric items.

Extra Credit

Extra credit ideas:
  • Modify your program to read in a series numbers from a text file or ask the user for one number, depending on what the user wants: +10 points
  • Print all possible combinations of prime numbers that solve the conjecture (commutative results aren’t required, e.g. 5+3 and 3+5 are the same combination in my view): +5 points

I will allow you to add something creative and interesting to your program for extra credit. Your extra credit additions needs to be relevant to the techniques we are learning. You are not guaranteed to receive extra credit, it will be up to the lab TA to decide. Any extra credit work you do needs to be clearly commented in your code. If your extra credit code is not clearly documented using comments, you risk losing points instead of receiving bonuses.

Extra credit is added your normal submission file, do not submit a second file. Extra credit supersedes any of the above grading rubric if it conflicts.