How do I get variable values out of a function?

Asked

Viewed 72 times

-1

I have a function, name rounds, with another function inside, and in this function that ta inside I have a declared variable, it calls pecas_comp, i need it in function rounds but am not getting into the deeper function.

    def computador_escolhe_jogada (n, m):
if n <= m:
    pecas_comp = n
    n = 0
else:
    a = 1 
    while a < m:
        resto = n - a
        if resto % (m + 1) == 0:
            n = n - a
            pecas_comp = a
            a = m
        else:
            a = a + 1
return (n, pecas_comp)


def rodadas(n, m):
   while n > 0:
       computador_escolhe_jogada(n, m)
       print("O computador tirou ", pecas_comp, "peças.")
       print("Agora restam ", n, "peças no tabuleiro.")
       comeca = 1
  • Hi John all good, please put the code you made so we can understand what has already been done and help explain how to get to the solution you want.

  • Friend, my code is 60 lines, because it is not only these two functions, however, this all interconnected. It has some problem I put everything?

  • It’s okay to put it all no, but if you have a way to be more objective by making a simplified example of the problem, it helps even more so that we can understand.

  • 1

    @John would just be you isolate these two functions.

  • Okay, I’ll edit it and put it.

1 answer

1


I don’t guarantee that the code is correct, but I solved the problem by returning the value I needed. There were other problems. Note that you don’t even need a variable for what you were wanting to do. Variable is overrated.

def computador_escolhe_jogada(n, m):
    if n <= m:
        pecas_comp = n
        n = 0
    else:
        a = 1 
        while a < m:
            resto = n - a
            if resto % (m + 1) == 0:
                n -= a
                pecas_comp = a
                a = m
            else:
                a += 1
    return pecas_comp

def rodadas(n, m):
    while n > 0:
        pecas_comp = computador_escolhe_jogada(n, m)
        print("O computador tirou ", pecas_comp, "peças.")
        print("Agora restam ", n, "peças no tabuleiro.")
    return 1 

def partida():
    n = int(input("Quantas peças?"))
    m = int(input("Limite de peças por jogada?"))
    if n // (m + 1) == 0:
        print("Você começa")
        return 1
    else:
        return 2

rodadas(n, m)

I put in the Github for future reference.


What is written below is only based on the original question that gave misleading information of what the code really was, so it stands for historical reasons and that serves another problem.

Like I do to have her value?

The most correct is to return this value from the inner function to the outer one. You can even return a tuple if you need to return more than one value. Are you sure you needed one function inside the other? Something tells me you don’t need.

The second most correct means is to declare a variable in the outermost function and use it in the inner to receive the value, as the variable was created in the function it has lifespan higher, the value will survive even in the scope of the outermost function.

If I turn it global, it works?

This would be the worst solution, but it is still one. Without a good justification I would not use so.

And once I declare the variable in global within a function I have to declare in the other functions also?

It is not necessary, at the moment that does this the variable is visible and has life time for every application, just do not do it, already realized the danger that is in large application?

The use of global should only be a last resort and only done by those who have a lot of experience. To give a comparison it is much easier to isolate a function from the rest of the problem and make a code that is a Minimum, Complete and Verifiable Example than dealing right with global variables.

  • So, I’m returning it, I even put in tuple now, but still error, this saying that the pecas_comp is not defined.

  • Now that you’ve entered the code, it’s not even the way you described it, and although you now have more information, it’s all played out without a clear description. What’s more, the code is extraordinarily confusing. I think you should start with more basic things, you’re doing something that you’re not able to organize because you’re skipping steps. I didn’t even understand the flow there, but maybe just because there are several mistakes.

  • Thanks, I managed to finish the program, I was having difficulties to enter the function and return the values, but the way you did, putting the function in the variable worked. I am doing an online course at USP and they passed this exercise, I also found it a little advanced, by messing with functions and tals but even so I was willing to try and do and I believe that the difficulties help in the learning process. Anyway, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.