4
\$\begingroup\$

This is a stock market game I made in Python a while ago, and I wanted to go ahead and share it on here. If you are wondering, the reason why there is only 2 stocks is due to the fact that creating a single stock is a long and dull process. Though, as I want to have a future in finance, this little text based game was fun to make.

import time
import sys
import random
#########3
class Player:

    def __init__(self, name):
        self.name = name
        self.money = 20
        pass

class Stock:

    def __init__(self, name):
        self.Stock = 0
        self.name = name
        self.value = 10
        self.price = self.value
        pass

p = Player("Player")
#########


pstock = Stock("Delta Airlines Stock")
cstock = Stock("Coleas Choclate Factory Stock")
###############


def Home():
    print("You are home.")
    print("Your money:")
    print p.money
    time.sleep(1)
    print("1.) Go to market")
    print("2.) Check what stocks you own")
    home = input("")
    if home == 1:
        market()
    if home == 2:
        stocklist()
def stockem():
    if cstock.Stock == 1:
        print("1 Coleas Choclate Factory Stock")
        stockshift()
    elif cstock.Stock == 2:
        print("2 Coleas Choclate Factory Stock")
        stockshift()
    elif cstock.Stock == 3:
        print("3 Coleas Choclate Factory Stock")
        stockshift()
    elif cstock.Stock == 4:
        print("4 Coleas Choclate Factory Stock")
        stockshift()
    elif cstock.Stock == 5:
        print("5 Coleas Choclate Factory Stock")
        stockshift()
def stocklist():
    if pstock.Stock == 1:
        print("1 Delta Airlines Stock")
        stockem()
    elif pstock.Stock == 2:
        print("2 Delta Airlines Stock")
        stockem()
    elif pstock.Stock == 3:
        print("3 Delta Airlines Stock")
        stockem()
    elif pstock.Stock == 4:
        print("4 Delta Airlines Stock")

        stockem()
    elif pstock.Stock == 5:
        print("5 Delta Airlines Stock")
        stockem()





    else:
        print("You dont own any stocks!")
        stockshift()

def stockshift():
    time.sleep(3)
    print("__________________")
    print("1.) Back")
    stockshift1 = input("")
    if stockshift1 == 1:
        Home()
    else:
        stockshift()


#########################
def market():
    delta = random.randint(1,3)
    if delta == 1:
        deltacash = random.randint(1,3)
        if deltacash == 1:
            pstock.value += 1
            finalmarket()
        if deltacash == 2:
            pstock.value += 2
            finalmarket()
        if deltacash == 3:
            pstock.value += 3
            finalmarket()
    elif delta == 2:
        deltalow = random.randint(1,3)
        if deltalow == 1:
            pstock.value -= 1
            finalmarket()
        elif deltalow == 2:
            pstock.value -= 2
            finalmarket()
        elif deltalow == 3:
            pstock.value -= 3
            finalmarket()
    elif delta == 3:
        finalmarket()
    bob = random.randint(1,3)
    if bob == 1:
        bobcash = random.randint(1,3)
        if bobcash == 1:
            cstock.value += 1
            finalmarket()
        elif bobcash == 2:
            cstock.value += 2
            finalmarket()
        elif bobcash == 3:
            cstock.value += 3
            finalmarket()
    elif bob == 2:
        boblow = random.randint(1,3)
        if boblow == 1:
            cstock.value -= 1
            finalmarket()
        elif boblow ==2:
            cstock.value -= 2
            finalmarket()
        elif boblow == 3:
            cstock.value -= 3
            finalmarket()
    elif bob == 3:
        finalmarket()

def finalmarket():
        print ("Your Cash:")
        print p.money
        time.sleep(3)
        print("Stock:")
        print pstock.name
        print("Value:")
        print pstock.value
        time.sleep(3)
        print("Stock:")
        print cstock.name
        print("Value:")
        print cstock.value
        time.sleep(3)
        print("1.) Buy Delta Airlines Stock")
        print("2.) Sell Delta Airlines Stock")
        print("3.) Leave")
        print("4.) Buy Coleas Choclate Factory Stock")
        print("5.) Sell Coleas Choclate Factory Stock")
        finalmark = input("")
        if finalmark == 1:
            time.sleep(3)
            print("How many would you like to buy? (Up to 5)")
            popsdelta = input("")
            if popsdelta == 1:
                pstock.Stock += 1
                p.money -= 10
                print("Transaction Successful")
                Home()
            elif popsdelta == 2:
                pstock.Stock +=2
                p.money -= 20
                print("Transaction Successful")
                Home()
            elif popsdelta == 3:
                pstock.Stock += 3
                p.money -= 30
                print("Transaction Successful")
                Home()
            elif popsdelta == 4:
                pstock.Stock += 4
                p.money -= 40
                print("Transaction Successful")
                Home()
            elif popsdelta == 5:
                pstock.Stock += 5
                p.money -= 50
                print("Transaction Successful")
                Home()
        elif finalmark == 2:
            print("How many would you like to sell? (Up to 5)")
            deltasell = input("")
            if deltasell == 1:
                pstock.Stock -= 1
                p.money += pstock.value
                print("Transaction Successful")
                Home()
            elif deltasell == 2:
                pstock.Stock -= 2
                p.money += pstock.value
                p.money += pstock.value
                print("Transaction Successful")
                Home()
            elif deltasell == 3:
                pstock.Stock -= 3
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                print("Transaction Sucessful")
                Home()
            elif deltasell == 4:
                pstock.Stock -= 4
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                print("Transaction Successful")
                Home()
            elif deltasell == 5:
                pstock.Stock -= 5
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                p.money += pstock.value
                print("Transaction Successful")
                Home()




        elif finalmark == 3:
            Home()
        elif finalmark == 4:
            print("How many would you like to buy?")
            ohgod = input("")
            if ohgod == 1:
                cstock.Stock += 1
                p.money -= cstock.value
                print("Transaction Successful")
                Home()
            elif ohgod == 2:
                cstock.Stock += 2
                p.money -= cstock.value
                p.money -= cstock.value
                print("Transaction Successful")
                Home()
            elif ohgod == 3:
                cstock.Stock += 3
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                print("Transaction Successful")
                Home()
            elif ohgod == 4:
                cstock.Stock += 4
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                print("Transaction Successful")
                Home()
            elif ohgod == 5:
                cstock.Stock += 5
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                p.money -= cstock.value
                print("Transaction Successful")
                Home()
        elif finalmark == 5:
            print("How many would you like to sell?")
            why = input("")
            if why == 1:
                cstock.Stock -= 1
                p.money += cstock.value
                print("Transaction Successful")
                Home()
            elif why == 2:
                cstock.Stock -= 2
                p.money += cstock.value
                p.money += cstock.value
                print("Transaction Successful")
                Home()
            elif why == 3:
                cstock.Stock -= 3
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                print("Transaction Successful")
                Home()
            elif why == 4:
                cstock.Stock -= 4
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                print("Transaction Successful")
                Home()
            elif why == 5:
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                p.money += cstock.value
                cstock.Stock -= 5
                print("Transaction Successful")
                Home()





        #pstock.value +=







Home()
\$\endgroup\$
1
\$\begingroup\$

Your market() function could use some refactoring. For example, your code could be condensed to:

def market():
    delta = random.randint(1,3)
    if delta == 1:
        deltacash = random.randint(1,3)
        pstock.value += deltacash
        finalmarket()
    elif delta == 2:
        deltalow = random.randint(1, 3)
        pstock.value -= deltalow
        finalmarket()
    else:
        finalmarket()

    bob = random.randint(1, 3)
    if bob == 1:
        bobcash = random.randint(1, 3)
        cstock.value += bobcash
        finalmarket()
    elif bob == 2:
        boblow = random.randint(1, 3)
        cstock.value -= bobcash
        finalmarket()
    else:
        finalmarket()

You can summarize the above code into "add a random value between -3 and 3" leading to:

def market():
    delta = random.randint(-3,3)
    pstock.value += delta
    finalmarket()

    bobcash = random.randint(-3, 3)
    cstock.value += bobcash
    finalmarket()

Cases where randomint is equal to 0 would result in pstock/cstock unchanged and a call to finalmarket() which is what you have in the final else statements for each case.

Overall, The same logic I used here could be used in other functions as well:

finalmarket() - math with deltas, repeated additions -> multiply, look into string .format() to avoid multiple print statements

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.