Writing Simple Programs

In this topic, we’ll be writing a computer program in Python.

Make sure you are familiar with how to use the IDLE development program, or just use your favorite development tools that you have discovered on your own (text editors, or other IDEs, if that is your choice.)

Your Assignment

I would like you to write an interactive Python calculator program.

  • The program should allow the user to type an arithmetic expression, and then print the value of the expression. A friendly message should prompt them for the expression they want to evaulate!

  • Include a definite loop so that the user can perform 100 different expression calculations. e.g.:

    Enter a mathematical expression: 1 + 1
    1 + 1 = 2
    Enter a mathematical expression: 3 * 3.1459
    3 * 3.1459 = 9.4377
    Enter a mathematical expression:
  • Your program should print a friendly introduction (one line is fine) that explains what it does when the program first runs.

  • Your program should be written inside a function called main(), as we’ve explained in class.

Note: to quit early, just allow the user crash the program by typing a bad expression or simply closing the window that the calculator program is running in. You’ll learn better ways of terminating interactive programs in later on.

Hint: All the parts you need to make a calculator should already be known to you from lecture!

Submission Requirements

In this course, I will try to instill best practices with regard to writing code. There are a few standards to adhere to when submitting your lab. Please read, and follow them carefully.

  1. Submission Filename: Your submission requires that you hand in a file called lab_writing_programs.py.

  2. Documentation String (“docstring”): Docstrings consist of three quotes ("""), a summary line, followed by a blank line, followed by a more elaborate description, and ending with three quotes. Docstrings are used to document your program automatically (using the help() function in Python or other tools:

    A properly edited version of this will be required to be the very first thing in your Python program file.

    """CSC 161 Lab: Writing Programs
    This program implements a basic interactive calculator.
    Fox Mulder
    Lab Section TR 2:00-3:15pm
    Fall 2015

    Read up on docstrings on the Program Documentation String page.

  3. Comments (lines prefaced with #): Comments only readable by humans, and completely ignored by Python when running. They are different from docstrings (which are used for documenting a program).

    If you do something that is not obvious in your code, or something you’d like to explain more thououghly to the grader, write some comments like:

    Unclear parts of your code can receive point deductions, so be judicious about using comments

    # This block comment explains that this
    # part of my code does something interesting.
    # Block comments should not be overused!
    x = 1 + 1

    Or, you can use inline comments:

    x = x + 1   # Increment x

    Read up on comments on the Program Comments page.

  4. Python Code Style: In general, follow Pythonic Coding Style, a set of coding guidelines to make your Python code more readable and maintainable. You will need to run your Python program file through the online PEP 8 style checker found in CSC 161 Style.

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.

Here’s a brief grading rubric:

  • A bad/missing docstring (i.e. no name, lab section, etc), -10 points
  • Syntax errors, -20 points
  • Doesn’t do a constant number of loops (like 100), -15 points
  • Doesn’t use the correct number of iterations in the for loop based on user input, -15 points
  • Missing main() function: -10 points
  • Code doesn’t attempt to adhere to coding styles, -5 points
  • The submitted filename is wrong, -5 points
Extra Credit:
  • Can you “pretty print” the calculation, instead of just printing the result? For example, printing a result of "3 * 4 = 12", instead of "12".

    If so, +5 points!

  • The assignment asks that you let your loop run, say 100 times. Can you change it so the user can tell the program how many calculations to evaulate?

    If so, +5 points!

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