python 2.7 - How would you code this? Should I use a loop or keep it how I have it coded? -
i have method nba basketball game simulation. method checks variable named self.quartermins tell if minutes in quarter done. changes index 2 different variables use in code tell quarter being played.
def checkquarter(self): # method checks , makes sure quarter , game aren't over. self.quarterindex = 0 if self.quartermins[0] > 0: self.quarterindex = 0 self.playermins = -4 elif self.quartermins[1] > 0: self.quarterindex = 1 self.playermins = -3 elif self.quartermins[2] > 0: self.quarterindex = 2 self.playermins = -2 elif self.quartermins[3] > 0: self.quarterindex = 3 self.playermins = -1 else: return false
the following method same:
def checkquarter(self): self.quarterindex = 0 in xrange(4): if self.quartermins[i] > 0: self.quarterindex = self.playermins = -4 + break else: return false
this uses loop let variable i
go 0 3 , it'll set self.quarterindex
, self.playmins
variables whenever if statement true. when that's case, it'll break out of loop using break
, method done. if doesn't happen @ python run else:
section that's part of loop - run when don't use break
in loop.
i'm not saying above code better shorten logic. can write (but i'm not sure whether that's better):
def checkquarter(self): self.quarterindex = 0 index = none in xrange(4): if self.quartermins[i] > 0: index = break if index none: return false self.quarterindex = index self.playermins = -4 + index
Comments
Post a Comment