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.

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.