mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -1,153 +1,171 @@
|
||||
import random
|
||||
|
||||
#Stock_Market
|
||||
class Stock_Market():
|
||||
|
||||
def __init__(self):
|
||||
# Stock_Market
|
||||
class Stock_Market:
|
||||
def __init__(self):
|
||||
|
||||
#Hard Coded Names
|
||||
short_names = ['IBM', 'RCA', 'LBJ', 'ABC', 'CBS']
|
||||
full_names = ['INT. BALLISTIC MISSLES', 'RED CROSS OF AMERICA',
|
||||
'LICHTENSTEIN, BUMRAP & JOKE', 'AMERICAN BANKRUPT CO.',
|
||||
'CENSURED BOOKS STORE']
|
||||
# Hard Coded Names
|
||||
short_names = ["IBM", "RCA", "LBJ", "ABC", "CBS"]
|
||||
full_names = [
|
||||
"INT. BALLISTIC MISSLES",
|
||||
"RED CROSS OF AMERICA",
|
||||
"LICHTENSTEIN, BUMRAP & JOKE",
|
||||
"AMERICAN BANKRUPT CO.",
|
||||
"CENSURED BOOKS STORE",
|
||||
]
|
||||
|
||||
#Initializing Dictionary to hold all the information systematically
|
||||
self.data = {}
|
||||
for sn, fn in zip(short_names, full_names):
|
||||
#A dictionary for each stock
|
||||
temp = {'Name' : fn, 'Price' : None, 'Holdings' : 0}
|
||||
#Nested outer dictionary for all stocks
|
||||
self.data[sn] = temp
|
||||
# Initializing Dictionary to hold all the information systematically
|
||||
self.data = {}
|
||||
for sn, fn in zip(short_names, full_names):
|
||||
# A dictionary for each stock
|
||||
temp = {"Name": fn, "Price": None, "Holdings": 0}
|
||||
# Nested outer dictionary for all stocks
|
||||
self.data[sn] = temp
|
||||
|
||||
#Initializing Randomly generated initial prices
|
||||
for stock in self.data.values():
|
||||
stock['Price'] = round(random.uniform(80,120),2) #Price b/w 60 and 120
|
||||
# Initializing Randomly generated initial prices
|
||||
for stock in self.data.values():
|
||||
stock["Price"] = round(random.uniform(80, 120), 2) # Price b/w 60 and 120
|
||||
|
||||
#Initialize Assets
|
||||
self.cash_assets = 10000
|
||||
self.stock_assets = 0
|
||||
|
||||
def total_assets(self):
|
||||
|
||||
return self.cash_assets + self.stock_assets
|
||||
# Initialize Assets
|
||||
self.cash_assets = 10000
|
||||
self.stock_assets = 0
|
||||
|
||||
def _generate_day_change(self):
|
||||
def total_assets(self):
|
||||
|
||||
self.changes = []
|
||||
for _ in range(len(self.data)):
|
||||
self.changes.append(round(random.uniform(-5,5),2)) #Random % Change b/w -5 and 5
|
||||
return self.cash_assets + self.stock_assets
|
||||
|
||||
def update_prices(self):
|
||||
def _generate_day_change(self):
|
||||
|
||||
self._generate_day_change()
|
||||
for stock, change in zip(self.data.values(), self.changes):
|
||||
stock['Price'] = round(stock['Price'] + (change/100)*stock['Price'], 2)
|
||||
self.changes = []
|
||||
for _ in range(len(self.data)):
|
||||
self.changes.append(
|
||||
round(random.uniform(-5, 5), 2)
|
||||
) # Random % Change b/w -5 and 5
|
||||
|
||||
def print_exchange_average(self):
|
||||
|
||||
sum = 0
|
||||
for stock in self.data.values():
|
||||
sum += stock['Price']
|
||||
def update_prices(self):
|
||||
|
||||
print('\nNEW YORK STOCK EXCHANGE AVERAGE: ${:.2f}'.format(sum/5))
|
||||
self._generate_day_change()
|
||||
for stock, change in zip(self.data.values(), self.changes):
|
||||
stock["Price"] = round(stock["Price"] + (change / 100) * stock["Price"], 2)
|
||||
|
||||
def get_average_change(self):
|
||||
def print_exchange_average(self):
|
||||
|
||||
sum = 0
|
||||
for change in self.changes:
|
||||
sum += change
|
||||
|
||||
return round(sum/5,2)
|
||||
sum = 0
|
||||
for stock in self.data.values():
|
||||
sum += stock["Price"]
|
||||
|
||||
def print_first_day(self):
|
||||
print(f"\nNEW YORK STOCK EXCHANGE AVERAGE: ${sum / 5:.2f}")
|
||||
|
||||
print('\nSTOCK\t\t\t\t\tINITIALS\tPRICE/SHARE($)')
|
||||
for stock, data in self.data.items():
|
||||
if stock != 'LBJ':
|
||||
print('{}\t\t\t{}\t\t{}'.format(data['Name'], stock, data['Price']))
|
||||
else:
|
||||
print('{}\t\t{}\t\t{}'.format(data['Name'], stock, data['Price']))
|
||||
def get_average_change(self):
|
||||
|
||||
self.print_exchange_average()
|
||||
self.print_assets()
|
||||
sum = 0
|
||||
for change in self.changes:
|
||||
sum += change
|
||||
|
||||
def take_inputs(self):
|
||||
return round(sum / 5, 2)
|
||||
|
||||
print('\nWHAT IS YOUR TRANSACTION IN')
|
||||
flag = False
|
||||
while flag != True:
|
||||
new_holdings = []
|
||||
for stock in self.data.keys():
|
||||
try:
|
||||
new_holdings.append(int(input('{}? '.format(stock))))
|
||||
except:
|
||||
print('\nINVALID ENTRY, TRY AGAIN\n')
|
||||
break
|
||||
if len(new_holdings) == 5:
|
||||
flag = self._check_transaction(new_holdings)
|
||||
|
||||
return new_holdings
|
||||
def print_first_day(self):
|
||||
|
||||
def print_trading_day(self):
|
||||
print("\nSTOCK\t\t\t\t\tINITIALS\tPRICE/SHARE($)")
|
||||
for stock, data in self.data.items():
|
||||
if stock != "LBJ":
|
||||
print("{}\t\t\t{}\t\t{}".format(data["Name"], stock, data["Price"]))
|
||||
else:
|
||||
print("{}\t\t{}\t\t{}".format(data["Name"], stock, data["Price"]))
|
||||
|
||||
print("STOCK\tPRICE/SHARE\tHOLDINGS\tNET. Value\tPRICE CHANGE")
|
||||
for stock, data, change in zip(self.data.keys(), self.data.values(),self.changes):
|
||||
value = data['Price'] * data['Holdings']
|
||||
print('{}\t{}\t\t{}\t\t{:.2f}\t\t{}'.format(stock, data['Price'], data['Holdings'], value, change))
|
||||
self.print_exchange_average()
|
||||
self.print_assets()
|
||||
|
||||
def update_cash_assets(self, new_holdings):
|
||||
def take_inputs(self):
|
||||
|
||||
sell=0
|
||||
buy=0
|
||||
for stock, holding in zip(self.data.values(), new_holdings):
|
||||
if holding > 0:
|
||||
buy += stock['Price']*holding
|
||||
|
||||
elif holding < 0:
|
||||
sell += stock['Price']*abs(holding)
|
||||
print("\nWHAT IS YOUR TRANSACTION IN")
|
||||
flag = False
|
||||
while flag != True:
|
||||
new_holdings = []
|
||||
for stock in self.data.keys():
|
||||
try:
|
||||
new_holdings.append(int(input(f"{stock}? ")))
|
||||
except:
|
||||
print("\nINVALID ENTRY, TRY AGAIN\n")
|
||||
break
|
||||
if len(new_holdings) == 5:
|
||||
flag = self._check_transaction(new_holdings)
|
||||
|
||||
self.cash_assets = self.cash_assets + sell - buy
|
||||
return new_holdings
|
||||
|
||||
def update_stock_assets(self):
|
||||
def print_trading_day(self):
|
||||
|
||||
sum=0
|
||||
for data in self.data.values():
|
||||
sum += data['Price']*data['Holdings']
|
||||
print("STOCK\tPRICE/SHARE\tHOLDINGS\tNET. Value\tPRICE CHANGE")
|
||||
for stock, data, change in zip(
|
||||
self.data.keys(), self.data.values(), self.changes
|
||||
):
|
||||
value = data["Price"] * data["Holdings"]
|
||||
print(
|
||||
"{}\t{}\t\t{}\t\t{:.2f}\t\t{}".format(
|
||||
stock, data["Price"], data["Holdings"], value, change
|
||||
)
|
||||
)
|
||||
|
||||
self.stock_assets = round(sum,2)
|
||||
def update_cash_assets(self, new_holdings):
|
||||
|
||||
def print_assets(self):
|
||||
sell = 0
|
||||
buy = 0
|
||||
for stock, holding in zip(self.data.values(), new_holdings):
|
||||
if holding > 0:
|
||||
buy += stock["Price"] * holding
|
||||
|
||||
print('\nTOTAL STOCK ASSETS ARE: ${:.2f}'.format(self.stock_assets))
|
||||
print('TOTAL CASH ASSETS ARE: ${:.2f}'.format(self.cash_assets))
|
||||
print('TOTAL ASSETS ARE: ${:.2f}'.format(self.total_assets()))
|
||||
elif holding < 0:
|
||||
sell += stock["Price"] * abs(holding)
|
||||
|
||||
def _check_transaction(self, new_holdings):
|
||||
self.cash_assets = self.cash_assets + sell - buy
|
||||
|
||||
sum = 0
|
||||
for stock, holding in zip(self.data.values(), new_holdings):
|
||||
if holding > 0:
|
||||
sum += stock['Price']*holding
|
||||
|
||||
elif holding < 0:
|
||||
if abs(holding) > stock['Holdings']:
|
||||
print('\nYOU HAVE OVERSOLD SOME STOCKS, TRY AGAIN\n')
|
||||
return False
|
||||
def update_stock_assets(self):
|
||||
|
||||
if sum > self.cash_assets:
|
||||
print('\nYOU HAVE USED ${:.2f} MORE THAN YOU HAVE, TRY AGAIN\n'.format(sum - self.cash_assets))
|
||||
return False
|
||||
sum = 0
|
||||
for data in self.data.values():
|
||||
sum += data["Price"] * data["Holdings"]
|
||||
|
||||
return True
|
||||
self.stock_assets = round(sum, 2)
|
||||
|
||||
def update_holdings(self, new_holdings):
|
||||
def print_assets(self):
|
||||
|
||||
print(f"\nTOTAL STOCK ASSETS ARE: ${self.stock_assets:.2f}")
|
||||
print(f"TOTAL CASH ASSETS ARE: ${self.cash_assets:.2f}")
|
||||
print(f"TOTAL ASSETS ARE: ${self.total_assets():.2f}")
|
||||
|
||||
def _check_transaction(self, new_holdings):
|
||||
|
||||
sum = 0
|
||||
for stock, holding in zip(self.data.values(), new_holdings):
|
||||
if holding > 0:
|
||||
sum += stock["Price"] * holding
|
||||
|
||||
elif holding < 0:
|
||||
if abs(holding) > stock["Holdings"]:
|
||||
print("\nYOU HAVE OVERSOLD SOME STOCKS, TRY AGAIN\n")
|
||||
return False
|
||||
|
||||
if sum > self.cash_assets:
|
||||
print(
|
||||
"\nYOU HAVE USED ${:.2f} MORE THAN YOU HAVE, TRY AGAIN\n".format(
|
||||
sum - self.cash_assets
|
||||
)
|
||||
)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def update_holdings(self, new_holdings):
|
||||
|
||||
for stock, new_holding in zip(self.data.values(), new_holdings):
|
||||
stock["Holdings"] += new_holding
|
||||
|
||||
for stock, new_holding in zip(self.data.values(), new_holdings):
|
||||
stock['Holdings'] += new_holding
|
||||
|
||||
def print_instruction():
|
||||
|
||||
print('''
|
||||
print(
|
||||
"""
|
||||
THIS PROGRAM PLAYS THE STOCK MARKET. YOU WILL BE GIVEN
|
||||
$10,000 AND MAY BUY OR SELL STOCKS. THE STOCK PRICES WILL
|
||||
BE GENERATED RANDOMLY AND THEREFORE THIS MODEL DOES NOT
|
||||
@@ -164,45 +182,47 @@ HAVE $10,000 TO INVEST. USE INTEGERS FOR ALL YOUR INPUTS.
|
||||
(NOTE: TO GET A 'FEEL' FOR THE MARKET RUN FOR AT LEAST
|
||||
10 DAYS)
|
||||
------------GOOD LUCK!------------\n
|
||||
''')
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print('\t\t STOCK MARKET')
|
||||
help = input('\nDO YOU WANT INSTRUCTIONS(YES OR NO)? ')
|
||||
|
||||
#Printing Instruction
|
||||
if help == 'YES' or help == 'yes' or help == 'Yes':
|
||||
print_instruction()
|
||||
|
||||
#Initialize Game
|
||||
Game = Stock_Market()
|
||||
print("\t\t STOCK MARKET")
|
||||
help = input("\nDO YOU WANT INSTRUCTIONS(YES OR NO)? ")
|
||||
|
||||
#Do first day
|
||||
Game.print_first_day()
|
||||
new_holdings = Game.take_inputs()
|
||||
Game.update_holdings(new_holdings)
|
||||
Game.update_cash_assets(new_holdings)
|
||||
print('\n------------END OF TRADING DAY--------------\n')
|
||||
# Printing Instruction
|
||||
if help == "YES" or help == "yes" or help == "Yes":
|
||||
print_instruction()
|
||||
|
||||
response = 1
|
||||
while response == 1:
|
||||
# Initialize Game
|
||||
Game = Stock_Market()
|
||||
|
||||
#Simulate a DAY
|
||||
Game.update_prices()
|
||||
Game.print_trading_day()
|
||||
Game.print_exchange_average()
|
||||
Game.update_stock_assets()
|
||||
Game.print_assets()
|
||||
|
||||
response = int(input('\nDO YOU WISH TO CONTINUE (YES-TYPE 1, NO-TYPE 0)? '))
|
||||
if response == 0:
|
||||
break
|
||||
|
||||
# Do first day
|
||||
Game.print_first_day()
|
||||
new_holdings = Game.take_inputs()
|
||||
Game.update_holdings(new_holdings)
|
||||
Game.update_cash_assets(new_holdings)
|
||||
print('\n------------END OF TRADING DAY--------------\n')
|
||||
print("\n------------END OF TRADING DAY--------------\n")
|
||||
|
||||
print('\nHOPE YOU HAD FUN!!!!')
|
||||
input('')
|
||||
response = 1
|
||||
while response == 1:
|
||||
|
||||
# Simulate a DAY
|
||||
Game.update_prices()
|
||||
Game.print_trading_day()
|
||||
Game.print_exchange_average()
|
||||
Game.update_stock_assets()
|
||||
Game.print_assets()
|
||||
|
||||
response = int(input("\nDO YOU WISH TO CONTINUE (YES-TYPE 1, NO-TYPE 0)? "))
|
||||
if response == 0:
|
||||
break
|
||||
|
||||
new_holdings = Game.take_inputs()
|
||||
Game.update_holdings(new_holdings)
|
||||
Game.update_cash_assets(new_holdings)
|
||||
print("\n------------END OF TRADING DAY--------------\n")
|
||||
|
||||
print("\nHOPE YOU HAD FUN!!!!")
|
||||
input("")
|
||||
|
||||
Reference in New Issue
Block a user