How to access variables defined in another function?

3

I'm writing a program about the NIM game. It asks me to call functions inside another function, but when I try to compute variables belonging to this function in another function (the function that calls the one that contains the variable) an error message appears saying that it does not identify the name of the variable. I tried every way to fix this, but I could not:

def computador_escolhe_jogada(n,m):
    m2 = 1
    while (m2 <= m) and (((n-m2)%(m+1)) != 0):
        m2 = m2 + 1
        return m2


def usuario_escolhe_jogada(n,m):  
    m1 = int(input('Quantas peças você vai tirar? '))
    while m1 > m:
        print()
        print('Oops! Jogada inválida! Tente de novo.')
        print()
    return m1
    print('O jogador removeu',m1,' peças')

def partida():

    n = int(input('n? '))
    m = int(input('m? '))
    while n > 0:
        if (n%(m+1) == 0):
            print('Você começa!!!')
            usuario_escolhe_jogada(n,m)
            print('Agora restam', n - m1,'peças no tabuleiro')

        if (n%(m+1)!=0):
            print('O computador começa')
            computador_escolhe_jogada(n,m)
            print('Agora restam', n - m2,'peças no tabuleiro')
        n = n - m1 - m2

It says that m1 is not defined, but it is set yes, only in the above function. How do I make the other function recognize it?

Hello everyone, thank you all for the contributions. It turns out that if I store m1 = user_shot_player (n, m) m2 = computer_shot_player (n, m)

The program simply performs the functions one more time and does not return the variables. And exercise requires that you do not use global variables (it's a requirement of exercise:)

    
asked by anonymous 20.10.2017 / 04:21

2 answers

3

When you define a variable within of a function, this variable exists only during the runtime of the function. In order for you to use the values of m1 or m2, they should be global variables, or stored in some way. Let's break it down:

def computador_escolhe_jogada(n,m):
    m2 = 1
    while (m2 <= m) and (((n-m2)%(m+1)) != 0):
        m2 = m2 + 1
        return m2

Its first function receives two parameters n and m and uses them to make the respective calculations inherent to the game and store in the m2 variable.

How to solve? :

Note that your functions return values, that is values , so you can store these values in variables. This way:

def computador_escolhe_jogada(n,m):
    m2 = 1
    while (m2 <= m) and (((n-m2)%(m+1)) != 0):
        m2 = m2 + 1
        return m2

m2 = computador_escolhe_jogada(10,20) # Valores hipotéticos 

In this way, the variable declared out of the function scope assumes the value of its function, in this case the value of m2 .

Okay, but how does this get in the way?

.
.
.
n = int(input('n? '))
m = int(input('m? '))
while n > 0:
    if (n%(m+1) == 0):
        print('Você começa!!!')
        m1 = usuario_escolhe_jogada(n,m)
        print('Agora restam', n - m1,'peças no tabuleiro')

    if (n%(m+1)!=0):
        print('O computador começa')
        m2 = computador_escolhe_jogada(n,m)
        print('Agora restam', n - m2,'peças no tabuleiro')
    n = n - m1 - m2

Your error was probably in this line: print('Agora restam', n - m1,'peças no tabuleiro') Note that the program could not call m1 because it was a variable that only existed within its functions

When you store the result of these functions in global variables:

m1 = usuario_escolhe_jogada(n,m)
m2 = computador_escolhe_jogada(n,m)

The program recognizes them and can deal with their values.

I hope I have helped you in some way.

    
20.10.2017 / 13:01
1

You could also choose to use "global" within the function. This would allow you to view the variable within that scope. Example:

def exemplo():
    global X 
    X = 'Valor da Variável'
    return X

In this case, if you print the value of the variable X, even if it is outside the function (if you have already executed the function, of course). It will have the value "Variable Value" that has been defined within the function.

    
24.10.2017 / 01:23