3
I’m writing a show about the NIM game. He asks me to call functions within 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 variable name. I tried every way to fix it, but I couldn’t:
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
He says that the m1
is not set, but it is set yes, only in the top function. How do I recognize the other function?
Hello everyone thanks for the contributions. It turns out that if I store M1 = user_choice(n,m) m2 = computer_choice played(n,m)
The program simply executes the functions once more and does not return the variables. And exercise requires no global variables (this is a requirement of exercise :)
In the current context, there is no
m1
. Why not passm1
andm2
as parameters?– Jefferson Quesado
Taking advantage, is there any way to properly format your code? Just select it and press Ctrl+k
– Jefferson Quesado
What game is this? What are the rules of this game ?
– Lacobus
Maybe it’s worth reading from: Python - NIM game
– Woss
Hello everyone, I am new to STACK OVERFLOW, this is my first question, sorry for the mistakes. Thanks for all your help. The rules of the game are as follows
– Guilherme Matos Passarini