Home > Software design >  Is there a way to take a value from a function without returning it?
Is there a way to take a value from a function without returning it?

Time:12-16

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?

def eng_Game(ra,rb):
    pointCount = 0
    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

CodePudding user response:

A possible solution is to have a global variable outside the function and change it inside the function for example

pointCount = 0

def engGame(ra,rb):
    global pointCount # define pointCount as a global variable
    # the rest of the function

engGame(a, b)
# pointCount will be changed here

CodePudding user response:

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.

CodePudding user response:

What you can do is passing the pointer to the variable you would like to return as an argument, and change the value of the variable in the function. This way, the variable will be updated without being returned.

  • Related