尝试在任何地方定义全局变量时,我得到“无效语法”

我目前正在尝试制作一个刽子手游戏。我已经在任何函数之外定义了变量“lives”,但是当尝试在 start_game 函数中使用它时,编辑器说该变量已定义但从未使用过。但是,当我尝试将它声明为全局时,无论它是在函数内部还是外部,它都会给我一个“无效语法”错误——特别是在赋值运算符“=”处。


import random


words = "dog cat log hog etc"     # <-- just a huge string of a bunch of words


words = words.split()


word = random.choice(words)



# Difficulties: Easy:12 Medium:9 Hard:6

lives = 0


current = "_" * len(word)



def gameLoop():

  while current != word and lives > 0:

    print("Guess a letter. If you wish to exit the game, enter 'exit'")

    input("")

    print(lives)



def start_game():

  while True:

    print("Welcome to Hangman! What game mode would you like to play? Easy, medium, or hard?")

    game_mode = str.lower(input(""))


   if game_mode == "easy":

      lives = 12

      gameLoop()

      break

    elif game_mode == "medium":

      lives = 9

      gameLoop()

      break

    elif game_mode == "hard":

      lives = 6

      gameLoop()

      break


start_game()


森林海
浏览 199回答 2
2回答

慕雪6442864

在我写这个问题的时候,我意识到我做错了什么,所以我决定继续自己回答这个问题。当您将变量定义为全局变量时,您不想像这样为变量分配一个变量:global&nbsp;lives&nbsp;=&nbsp;0那会给你一个错误。为什么?当你想将一个变量定义为全局变量时,你是在告诉计算机,“嘿,这里的这个变量是全局使用的,而不是本地使用的。”&nbsp;上面这行代码的问题在于,您还为变量分配了一个值,而此时您应该做的就是告诉计算机该变量是全局的。如果你想给变量赋值(无论是第一次还是重新赋值),那么你需要在不同的代码行上赋值。当我查找这个时,我没有发现任何明确的说法,所以我希望这有助于任何使用 python 编码的新人或者像我一样忘记它是如何工作的人。

狐的传说

首先,global语句是声明,而不是可执行语句。它只是告诉解释器查看模块命名空间而不是函数调用命名空间。它只需要在函数内部使用。在外面,本地和全局命名空间是同一个东西(模块命名空间),所以该global语句什么都不做。该语句必须是关键字,global后跟要被视为全局名称的逗号分隔列表。如果要为任何名称赋值,无论是否为全局名称,都必须在单独的赋值语句中进行。您可能想要更像下面的代码的东西,它会按照您的需要“工作”(我意识到这只是开发中的部分程序)。我修复了缩进以符合 PEP 8,因为我的老眼睛发现否则很难阅读代码!import randomwords = "tom dick harry".split()word = random.choice(words)# Difficulties: Easy:12 Medium:9 Hard:6lives = 0current = "_" * len(word)def gameLoop():&nbsp; &nbsp; global lives&nbsp; &nbsp; while current != word and lives > 0:&nbsp; &nbsp; &nbsp; &nbsp; print("Guess a letter. If you wish to exit the game, enter 'exit'")&nbsp; &nbsp; &nbsp; &nbsp; input("")&nbsp; &nbsp; &nbsp; &nbsp; print(lives)def start_game():&nbsp; &nbsp; global lives&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; print(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Welcome to Hangman! What game mode would you like to play? Easy, medium, or hard?"&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; game_mode = str.lower(input(""))&nbsp; &nbsp; &nbsp; &nbsp; if game_mode == "easy":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lives = 12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameLoop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif game_mode == "medium":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lives = 9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameLoop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif game_mode == "hard":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lives = 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameLoop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; breakstart_game()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python