-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapitalist_conrad.py
43 lines (36 loc) · 1.42 KB
/
capitalist_conrad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
CP1404/CP5632 - Practical
Capitalist Conrad wants a stock price simulator for a volatile stock.
The price starts off at $10.00, and, at the end of every day there is
a 50% chance it increases by 0 to 10%, and
a 50% chance that it decreases by 0 to 5%.
If the price rises above $1000, or falls below $0.01, the program should end.
The price should be displayed to the nearest cent (e.g. $33.59, not $33.5918232901)
"""
import random
MAX_INCREASE = 0.175 # 17.5% ; initially: 10%
MAX_DECREASE = 0.05 # 5%
MIN_PRICE = 1.0 # initially: 0.01
MAX_PRICE = 100.0 # initially: 1000
INITIAL_PRICE = 10.0
OUTPUT_FILE = "stock_output.txt"
out_file = open(OUTPUT_FILE, 'w')
price = INITIAL_PRICE
day = 0
print("Starting price: ${:,.2f}".format(price), file=out_file)
while price >= MIN_PRICE and price <= MAX_PRICE:
price_change = 0
day += 1
# generate a random integer of 1 or 2
# if it's 1, the price increases, otherwise it decreases
if random.randint(1, 2) == 1:
# generate a random floating-point number
# between 0 and MAX_INCREASE
price_change = random.uniform(0, MAX_INCREASE)
else:
# generate a random floating-point number
# between negative MAX_DECREASE and 0
price_change = random.uniform(-MAX_DECREASE, 0)
price *= (1 + price_change)
print("On day {} price is: ${:,.2f}".format(day, price), file=out_file)
out_file.close()