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:
- Ask the user for the starting price per share of a stock, and how many
shares the user wants to buy.
- Print out the value of the shares, and the total price charged by the broker
for buying the shares of the stock. (The value of the shares will be lower than the price charged.)
- Ask the user for the ending price per share of the stock (this might be lower or higher --- the stock
could have gone up or down).
- Print out the new value of the shares, and the amount of money the broker will give you back
after selling all the shares of the stock at the new price. (The value of the shares will be more than
what the broker gives you back.)
- Print out the amount of money the user made (profit) by buying and selling the stock. The amount will be positive
if they made money (they sold the stock for more than they paid), but it will be negative if they lost money
(they sold the stock for less than they paid).
- Print out the amount of money you made as a percent, relative to how much you paid. For instance,
in the example above, you paid $102 for the stock and got back $147 when you sold it, for a $45 profit.
Percentage-wise, this is 45/102 * 100 = about a 44% increase.
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.