Немного фондовом рынке игра в Python


Это игра на бирже я сделал в Python некоторое время назад, и я хотел бы идти вперед и поделиться им здесь. Если вы задаетесь вопросом, почему есть только 2 акции обусловлена тем, что создание единой акции-это долгий и скучный процесс. Хотя, как я хочу иметь будущее в области финансов, этот маленький текст на основе игры было весело, чтобы сделать.

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()


884
4
задан 30 марта 2018 в 06:03 Источник Поделиться
Комментарии
1 ответ

Ваш market() функция может использовать некоторые рефакторинга. Например, ваш код может быть сжат до:

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()

Вы можете обобщить приведенный выше код в "Добавить случайное значение между -3 и 3", что приведет к:

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

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

Случаях, когда randomint равна 0 приведет к pstock/cstock неизменной и Вызов finalmarket() что вы имеете в финале еще заявлений для каждого случая.

В целом, по той же логике я, может быть использован в других функциях:

finalmarket() - математика с дельты, неоднократные дополнения -> умножаем, смотрим в строку .format() чтобы избежать многократной печати заявления

1
ответ дан 31 марта 2018 в 05:03 Источник Поделиться