I need help making my Python code easier to read. My friend and I are creating a stock market simulator. This is the function for the buying mechanic.
The purpose of this section is to simulate a buying process. If the user wants to buy a specific stock, they just type its name into the terminal. It then asks them the quantity How much they want to buy. Once the input is given, its total value is stored in a variable called 'SessionCost' by multiplying the choice by the stock value, in this case, it is SNAP. It then checks if the balance is more or less than SessionCost
. If more, it subtracts the SessionCost
variable from from the total balance, and then adds the quantity chosen to the 'OwnedSnap' variable.
if choice == '1':
clear()
print("What company would you like to invest in?")
time.sleep(1)
choice = input('Enter here: ')
if choice == 'return':
clear()
elif choice == 'snap':
if balance < SNAP:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * SNAP
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedSnap += choiceInt
clear()
I had to copy and paste this code throughout 15 different stock choices, and it is impossible to work with and read if a sudden error would show up. I imagine I can use functions and classes to solve it myself but I am not too familiar with them and have bad experiences with them on other projects.
The fifteen stock choices are listed under # investements
.
For the owned stocks, I just used 'ownedSnap' and replaced Snap with the stock name, and assigned all of their values to 0.
Full code:
# script.modules
import time
from os import system
def clear():
system("clear")
from colorama import Fore
import random
balance = 500
# investments
SNAP = 84.55
AAPL = 145.76
TWTR = 62.25
TSLA = 759.05
NFLX = 587.55
FB = 363.05
MSFT = 299.50
DIS = 182.87
GPRO = 9.50
SUBX = 113.41
F = 13.49
BABA = 160.00
BAC = 40.40
GE = 100.30
GOOGL = 2811.00
#Owned
OwnedSnap = 0
OwnedAapl = 0
OwnedTwtr = 0
OwnedTsla = 0
OwnedNflx = 0
OwnedFb = 0
OwnedMsft = 0
OwnedDis = 0
OwnedGpro = 0
OwnedSubx = 0
OwnedF = 0
OwnedBaba = 0
OwnedBac = 0
OwnedGe = 0
OwnedGoogl = 0
# script.start
print("Loading dashboard..")
time.sleep(2)
clear()
while True:
print(Fore.GREEN,"Stock Simulator\nby William & Bryson")
print("")
print(Fore.WHITE,"1 -> Invest\n 2 -> Stock Options\n 3 -> Sell\n 4 -> Owned Stock\n 5 -> About\n 6 -> current balance")
choice = input("> ")
if choice == "options":
clear()
print("Option:\n'invest NAME 00' - invests in choosen company.\n'bank' - opens BOS dashboard. ")
choice = input("\nType return to go back > ")
if choice == 'return' or '> ':
clear()
pass
if choice == '1':
clear()
print("What company would you like to invest in?")
time.sleep(1)
choice = input('Enter here: ')
if choice == 'return':
clear()
elif choice == 'snap':
if balance < SNAP:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(1)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * SNAP
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedSnap += choiceInt
clear()
clear()
elif choice == 'aapl':
if balance < AAPL:
print(Fore.RED, 'You do not have enough money to buy this ')
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * AAPL
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedAapl += choiceInt
print(f'You bought {choice} items. your remaining balance is {balance}')
clear()
elif choice == 'twtr':
if balance < TWTR:
print(Fore.RED, 'You do not have enough money to buy this ')
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * TWTR
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedTwtr += choiceInt
print(f'You bought {choice} items. your remaining balance is {balance}')
clear()
elif choice == 'tsla':
if balance < TSLA:
print(Fore.RED, 'You do not have enough money to buy this ')
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * TSLA
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedTsla += choiceInt
print(f'You bought {choice} items. your remaining balance is {balance}')
clear()
elif choice == 'nflx':
if balance < NFLX:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * NFLX
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedNflx += choiceInt
clear()
elif choice == 'fb':
if balance < FB:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * FB
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedFb += choiceInt
clear()
elif choice == 'msft':
if balance < MSFT:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * MSFT
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedMsft += choiceInt
clear()
elif choice == 'dis':
if balance < DIS:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * DIS
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedDis += choiceInt
clear()
elif choice == 'gpro':
if balance < GPRO:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * GPRO
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedGpro += choiceInt
clear()
elif choice == 'subx':
if balance < SUBX:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * SUBX
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedSubx += choiceInt
clear()
elif choice == 'f':
if balance < F:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * F
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedF += choiceInt
clear()
elif choice == 'baba':
if balance < BABA:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * BABA
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedBaba += choiceInt
clear()
elif choice == 'bac':
if balance < BAC:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * BAC
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedBac += choiceInt
clear()
elif choice == 'ge':
if balance < GE:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * GE
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedGe += choiceInt
clear()
elif choice == 'googl':
if balance < GOOGL:
print(Fore.RED, 'You do not have enough money to buy this ')
time.sleep(2)
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
choice = input('Enter buying quantity: ')
choiceInt = int(choice)
SessionCost = choiceInt * GOOGL
clear()
if balance < SessionCost:
print(Fore.RED, 'You do not have enough money to buy this ')
choice = input('Type Return to go back > ')
if choice == 'return':
clear()
else:
balance -= SessionCost
OwnedGoogl += choiceInt
clear()
else:
print(Fore.RED, 'Invalid Entry > ')
clear()
pass
if choice == '2':
clear()
print("Here is a list of stock options..")
print(Fore.GREEN, f'SNAP = {SNAP}\n AAPL = {AAPL}\n TWTR = {TWTR}\n TSLA = {TSLA}\n NFLX = {NFLX}\n FB = {FB}\n MSFT = {MSFT}\n DIS = {DIS}\n GPRO = {GPRO}\n SUBX = {SUBX}\n F = {F}\n BABA = {BABA}\n BAC = {BAC}\n GE = {GE}\n GOOGL = {GOOGL}')
choice = input("\nType Return to go back > ")
if choice == 'return':
clear()
pass
if choice == '3':
clear()
print("What stock would you like to sell? > ")
print() # owned stock
choice = input("> ")
if choice == 'return':
clear()
pass
if choice == '5':
clear()
print("Welcome to Stock Simulator")
#print("")
#print("")
choice = input("\nType Return to go back > ")
if choice == 'return':
clear()
pass
if choice == '4':
clear()
print(f'Owned Stock:\n SNAP = {OwnedSnap}\n AAPL = {OwnedAapl}\n TWTR = {OwnedTwtr}\n TSLA = {OwnedTsla}\n NFLX = {OwnedNflx}\n FB = {OwnedFb}\n MSFT = {OwnedMsft}\n DIS = {OwnedDis}\n GPRO = {OwnedGpro}\n SUBX = {OwnedSubx}\n F = {OwnedF}\n BABA = {OwnedBaba}\n BAC = {OwnedBac}\n GE = {OwnedGe}\n GOOGL = {OwnedGoogl}')
choice = input("\nType Return to go back > ")
if choice == 'return':
clear()
pass
if choice == '6':
clear()
print(f'Current Balance is {balance}')
choice = input("\nType Return to go back > ")
if choice == 'return':
clear()