COMP 141 Project 1: Stock Market

In this project, you will write a program to process a stock market transaction. In the stock market, people buy and sell shares of various companies. A person will buy shares of a company's stock hoping that the value of the shares will increase so that they can be sold for more money than what the person paid for them. Naturally, there is a risk that the value of the stock will fall, and the person may have to sell the shares for less than what they paid, thereby losing money.

An individual person usually buys stocks through a brokerage organization that charges a commission (a small amount of money) to handle the stock transaction. We will assume that your broker charges you 2% of the total transaction as the commission. As an example, let's say you want to buy buy 100 shares of a stock that costs $1 per share. In this situation, your broker will charge you $100 to pay for the shares of stock (that's 100 shares multiplied by $1 per share), plus another $2 (that's 2% of $100) for a total price of $102.

Now let's say that some time later, the stock has increased in value and is now worth $1.50 per share. When you ask your broker to sell the stock, you will get back $150 for the value of the stock itself (that's 100 times $1.50) minus the 2% commission again. The commission now is 2% of $150, which is $3, so you get back $147, rather than the full $150.

What you need to do

Start a new Python program (in a separate window, not the Python shell). Put a comment at the top with your name and a description of what the program does. Write the program so that it does the following:

Sample Interactions

What the computer displays (prints) is in regular text, what the user types is in bold, and what the program is doing behind the scenes is in italics.

Test 1

(Program begins) What is the starting price per share in dollars? 1 How many shares do you want to buy? 100 The starting value of this stock is 100.0 dollars, and you will be charged 102.0 dollars after the commission. What is the ending price per share in dollars? 1.50 The ending value of this stock is 150.0 dollars, and you will receive 147.0 dollars after the commission. Your profit is 45.0 dollars, for a 44.11764705882353 percent increase in value. (Program ends)

Test 2

(Program begins) What is the starting price per share in dollars? 12.50 How many shares do you want to buy? 15 The starting value of this stock is 187.5 dollars, and you will be charged 191.25 dollars after the commission. What is the ending price per share in dollars? 13 The ending value of this stock is 195.0 dollars, and you will receive 191.1 dollars after the commission. Your profit is -0.15000000000000568 dollars, for a -0.07843137254902258 percent increase in value. (Program ends) (Don't worry about the extra decimal places that showed up in the profit. This is due to something called round-off error, and it happens because computers use approximations when storing decimal numbers.)
Your code does not need to follow this script verbatim, but all the mentioned functionality should work as shown.

How to get started

  • Create a Python program named stock_yourLastName_yourFirstName.py
  • Follow the instructions for commenting your code.
  • Submit your Python file on Moodle under Project 1.
  • Requirements

    A good program will do all of the following:

  • Program file is named correctly.
  • Include the standard program header as a comment at the top of your program.
  • Prompt the user for the starting price per share.
  • Prompt the user for the number of shares they want to buy.
  • Calculate the starting value of the stock and the price the user will be charged after commission and output both of these totals.
  • Prompt the user for the ending price per share.
  • Calculate the ending value of the stock and the amount the user will receive after commission and output both of these totals.
  • Calculate the profit/loss in dollars and the percent increase/decrease in value of the stock and output both of these values.
  • Code is commented appropriately, is neatly and clearly formatted, and it includes proper use of white space.
  • Hints

    Work out some examples on paper first to determine how the math works in this problem. Decide on what variables you need, what they represent in the problem, and what their data types should be. Test your program on lots of examples and make sure the math checks out.

    What to turn in

    Through Moodle, turn in your code as a file called stock_yourLastName_yourFirstName.py.

    Challenge Problems

    From time to time, I will offer "challenge problems" on assignments. These problems are designed to have little (but some) impact on your grade whether you do them or not. You should think of these problems as opportunities to work on something interesting and optional, rather than a way to raise your grade through "extra credit."

    Policy on challenge problems:

    Challenge problems for this assignment:

    Grading

    Your program will be graded on correctness (whether your calculations are correct), as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation, and comments (this is not an exhaustive list).

    You will receive one bonus point for every complete day your program is turned in early, up to a maximum of five points. For instance, if your program is due on September 20 at 11:59pm, if you turn in your code on Moodle any time on September 19 from 12:00am through 11:59pm, you will receive one extra point on your project. Programs submitted on September 18 from 12:00am through 11:59pm will receive two points. This pattern continues for up to five points.