我想知道是否有更好的方法这样做:我正在检查计算是否为0,如果0没有任何反应。如果不是0钱被添加到玩家。问题是我现在必须检查15种不同的股息计算,这怎么会很聪明?我可以做15个if / else语句,但那会很混乱。我的一个想法是让下面的代码,但如果语句为真,继续循环也检查其他语句,如果真的对他们采取行动。这在Python中可行吗?
我的另一个想法是做一个while循环,基本上每60秒,只是为所有股票设置分配,即使分红为0,但我认为如果玩家在执行期间做了其他事情,这有机会成为一个错误。一些示例代码: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()