I'm wondering if there is a better way doing this: I'm checking to see if a calculation is 0, if 0 nothing happens. If not 0 money gets added to player. The problem is that I have to check 15 different calculations of dividends right now, how would this be smart to do? I could do 15 if/else statements but that would be messy. My one idea was to have the code below but if the statements true, to continue the loop to also check the other statements and if true act on them. Is this possible in Python?
My other idea was to do a while loop and basically every 60 seconds, just setattr the dividens for all stocks even if the dividens are 0, but I think this has a chance to become a bug if the player does something else during the execution. Some sample code:
import random
import sys
import time
import os
def playerStatus():
playerStatus.playerMoney = 10000
playerStatus.playerLoan = 0
def dividends():
while True:
networthCalc()
pm = playerStatus.playerMoney
print("cash:", pm)
dividends.aapl = networthCalc.aapl / random.randint (20,30)
dividends.goog = networthCalc.goog / random.randint (20,30)
time.sleep(1)
if dividends.aapl > 0:
newCash = dividends.aapl + pm
setattr(playerStatus, "playerMoney", newCash)
print("aapl payed div:", dividends.aapl)
elif dividends.goog > 0:
newCash = dividends.goog + pm
setattr(playerStatus, "playerMoney", newCash)
print("goog payed div:", dividends.goog)
else:
pass
def networthCalc():
networthCalc.aapl = getattr(stockPrice, "aapl") * getattr(showAssets, "aapl")
networthCalc.goog = getattr(stockPrice, "goog") * getattr(showAssets, "goog")
def showAssets():
showAssets.aapl = 10
showAssets.goog = 10
def stockPrice():
stockPrice.aapl = 200
stockPrice.goog = 200
playerStatus()
showAssets()
stockPrice()
networthCalc()
dividends()
Aucun commentaire:
Enregistrer un commentaire