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.
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.
– Eduardo
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?
– João
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.
– Eduardo
@John would just be you isolate these two functions.
– Maniero
Okay, I’ll edit it and put it.
– João