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:)