NameError: name ‘n’ is defined but I don’t know what to do (new to coding)

huangapple go评论67阅读模式
英文:

NameError: name 'n' is not defined but i dont know what to do(new to coding)

问题

I added this to my minesweeper game so I could choose different difficulties yet when I run it, it returns:

NameError: name 'n' is not defined

def Game():

    difficulty = input("Select your difficulty (b, i, h):")
    if difficulty.lower() == 'b':
        n = 5
        mines_no = 5
    elif difficulty.lower() == 'i':
        n = 6
        mines_no = 10
    else:
        n = 8
        mines_no = 20

if __name__ == "__main__":

    Game()

    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)]
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]

So I expected that after I ran this code, the game would just start, because before it did start (when I didn't have the difficulty select screen yet), instead I had:

if __name__ == "__main__":

    n = 5
    mines_no = 5

    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)]
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]
英文:

i added this to my minesweeper game so i could choose different difficulties yet when i run it it returns:
NameError: name 'n' is not defined

def Game():
        
        difficulty = input("Select your difficulty (b, i, h):")
        if difficulty.lower() == 'b':
            n = 5
            mines_no = 5
        elif difficulty.lower() == 'i':
            n = 6
            mines_no = 10
        else:
            n = 8
            mines_no = 20 
 
if __name__ == "__main__":
    
    Game()
 
    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)] 
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]

so i expected after i ran set code that the game would just start because before it did start(when i didn't have the difficulty select screen yet)
instead i had

if __name__ == "__main__":
    
    n = 5
    mines_no = 5
 
    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)] 
    # The apparent values of the grid
    mine_values = [[' ' for y in range(n)] for x in range(n)]

答案1

得分: 1

你需要了解本地变量和全局变量之间的区别;一旦你将代码包装在 def Game(): 中,你就在操作本地变量 nmines_no,需要返回它们。

这样做后,最好将整个游戏封装在一个函数中,这样你就不会混合使用全局变量和局部变量(这是良好的编码习惯)。我还在 get_difficulty 函数中将这两个值作为元组返回,并在新的主 game() 函数中进行解包。

def get_difficulty():
    difficulty = input("选择难度 (b, i, h):")
    if difficulty.lower() == "b":
        return (5, 5)
    elif difficulty.lower() == "i":
        return (6, 10)
    else:
        return (8, 20)


def game():

    n, mines_no = get_difficulty()

    # 网格的实际值
    numbers = [[0 for y in range(n)] for x in range(n)]
    # 网格的显示值
    mine_values = [" " for y in range(n)] for x in range(n)]


if __name__ == "__main__":
    game()

(Note: I've translated the code comments as well for context.)

英文:

You'll need to read up on the differences between local and global variables; once you wrap things into def Game():, you're writing to local n and mines_no and you'd need to e.g. return them.

Once you do that, it's better to wrap your entire game in a function, so you're not mixing and matching globals and locals (it's good coding hygiene). I also took the liberty of just returning the two values as a tuple from get_difficulty, and unpacking them in the new main game() function.

def get_difficulty():
    difficulty = input("Select your difficulty (b, i, h):")
    if difficulty.lower() == "b":
        return (5, 5)
    elif difficulty.lower() == "i":
        return (6, 10)
    else:
        return (8, 20)


def game():

    n, mines_no = get_difficulty()

    # The actual values of the grid
    numbers = [[0 for y in range(n)] for x in range(n)]
    # The apparent values of the grid
    mine_values = [[" " for y in range(n)] for x in range(n)]


if __name__ == "__main__":
    game()

答案2

得分: 0

The variable n is inside the Game function.
If you return the values, you can use them:

def Game():
    difficulty = input("选择难度 (b, i, h):")
    if difficulty.lower() == 'b':
        n = 5
        mines_no = 5
    elif difficulty.lower() == 'i':
        n = 6
        mines_no = 10
    else:
        n = 8
        mines_no = 20 
    return n, mines_no

if __name__ == "__main__":
    n, mines_no = Game()
英文:

The variable n is inside the Game function.
If you return the values, you can use them:

def Game():
    difficulty = input("Select your difficulty (b, i, h):")
    if difficulty.lower() == 'b':
        n = 5
        mines_no = 5
    elif difficulty.lower() == 'i':
        n = 6
        mines_no = 10
    else:
        n = 8
        mines_no = 20 
    return n, mines_no
 
if __name__ == "__main__":
    
    n, mines_no = Game()

huangapple
  • 本文由 发表于 2023年4月17日 21:07:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035502-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定