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. One buys
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 the purchase price.
Individual investors usually buy stocks through a brokerage organization that charges
a commission (a small amount of money) to handle the stock transaction. We will assume
that the broker charges 2% of the total transaction. This means that if you want to
buy 100 shares of a stock that costs $1 per share, your broker will charge you the
100 times $1 = $100 to pay for the stock itself, plus 2% of $100 ($2) for a total of $102.
Let's say the stock value increases and is now worth $1.50 per share. When you sell the
stock, you get 100 times $1.50 = $150 for the stock itself, minus the 2%
commission again. The commission now is 2% of $150, which is $3, so you get back $147.
What you need to do
Write a program that 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 to make $147, for a $45 profit. Percentage-wise, this is 45/102 * 100 = about
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
Use the procedure we discussed in class for writing programs: before even thinking about code, write down the algorithm in pseudocode, and decide on what variables you will need, what their names will be, what their data types will be, and most importantly, what each variable represents in the program.
What to turn in
Through Moodle, turn in your code as a file called stock_yourLastName_yourFirstName.py.