I am programming code for a squash game scoring system and already return 2 values. If i return another value it would mess up other functions in my code. So i am wondering if there is any way to access the pointCount value outside of the function without returning it?
I don’t recommend doing this, but a quick way is to use a global variable, you will have to declare it first
pointCount = 0
def eng_Game(ra,rb):
global pointCount
win = False
winCondition = 9
aServer = False
bServer = False
aScore = 0
bScore = 0
serverNum = random.randint(1,2)
if serverNum == 1:
aServer = True
else: bServer = True
while win == False:
pointCount += 1
ProbAWin = ra/(ra+rb)
ProbBWin = rb/(rb+ra)
ranNum = random.random()
if ranNum < ProbAWin and aServer:
aScore += 1
elif ranNum < ProbAWin and aServer == False:
aServer = True
bServer = False
elif ranNum > ProbAWin and bServer:
bScore += 1
elif ranNum > ProbAWin and bServer == False:
bServer = True
aServer = False
if aScore == winCondition or bScore == winCondition:
return aScore,bScore
elif aScore == 8 and bScore == 8:
playTo = random.randint(1,2)
if playTo == 1:
winCondition = 10
You will be able to access it using the pointCount
variable outside the function now.
The proper way to do this would be to declare a class and initialise variables inside the class, then use the self.variable names to access and modify them. This is a good place to start, https://www.w3schools.com/python/python_classes.asp the syntax is quite easy to understand.