【问题标题】:Jython/Python - cannot retain variables between functionsJython/Python - 不能在函数之间保留变量
【发布时间】:2015-10-13 11:46:15
【问题描述】:

为单人编写一个非常基本的基于文​​本的游戏... 尝试拥有一个全局变量或“键”,如果已“找到”,则允许访问某些区域 IE。当找到键= 1。尝试使用以下代码访问房间时尝试确定结果的所有权(或其他): (我只会包含相关的代码部分)

def playGame(): 
  key = 0 
  location = "Porch"
  showIntroduction()
  while not (location == "Exit") :
    showRoom(location)
    direction = requestString("Which direction?")
    location = pickRoom(direction , location)

def showRoom(location):
  if location == "Porch":
    showPorch()
  if location == "Entryway":
    showEntryway()
 if location == "Kitchen":
    if direction == "north":
      return "DiningRoom"
    if direction == "west":
      if key == 1:
        return "Stairs"
      else:
        printNow("You do not possess the Skeleton Key!")
        return "Kitchen"
    if direction == "south":
      return "Entryway"

def showLR():
  if key == 0:
    printNow("...")
    printNow("You enter the living room; dust covers the fireplace.")
    printNow("Cobwebs shield a great armchair and it's contents from view.")
    printNow("You spot an object glinting on the mantle, the dim light     casting flickering shadows all around.")
    printNow("The only exit lies behind, to the west")
    printNow("...")
  if key == 1:
    printNow("...")
    printNow("The living room shimmers in the dappled light.")
    printNow("Cobwebs shield a great armchair and it's contents from view.")
    printNow("The key in your pocket feels warm, or is it just your imagination?")
    printNow("The only exit lies behind, to the west")
    printNow("...")

希望以上内容有意义,大多数函数 (showEntryway() showPorch()) 已被省略,但它们的作用与 showLR() 相似。

每次我进入 LivingRoom 并运行时,我都试图让“密钥”作为变量工作:

if key == 0:

我收到一条错误消息,提示未找到变量,初始值不是从 playGame()...\

希望这是可以理解的,任何帮助将不胜感激!!!!谢谢 TG

【问题讨论】:

    标签: python jython jes


    【解决方案1】:

    您有 2 个选择:将键设为 global 变量,或将其作为第二个参数传入。

    对于全局样式,您需要在要更新它的函数中有一个global key 行(即查找密钥)。

    全局样式:

    key = 0
    
    def playGame(): 
      global key
      key = 0
      location = "Porch"
      showIntroduction()
      while not (location == "Exit") :
        showRoom(location)
        direction = requestString("Which direction?")
        location = pickRoom(direction , location)
    
    def showRoom(location):
      if location == "Porch":
        showPorch()
      if location == "Entryway":
        showEntryway()
     if location == "Kitchen":
        if direction == "north":
          return "DiningRoom"
        if direction == "west":
          if key == 1:
            return "Stairs"
          else:
            printNow("You do not possess the Skeleton Key!")
            return "Kitchen"
        if direction == "south":
          return "Entryway"
    
    def showLR():
      if key == 0:
        printNow("...")
        printNow("You enter the living room; dust covers the fireplace.")
        printNow("Cobwebs shield a great armchair and it's contents from view.")
        printNow("You spot an object glinting on the mantle, the dim light     casting flickering shadows all around.")
        printNow("The only exit lies behind, to the west")
        printNow("...")
      if key == 1:
        printNow("...")
        printNow("The living room shimmers in the dappled light.")
        printNow("Cobwebs shield a great armchair and it's contents from view.")
        printNow("The key in your pocket feels warm, or is it just your imagination?")
        printNow("The only exit lies behind, to the west")
        printNow("...")
    

    或者,您可以维护一个键变量,并将其传递给每个函数(“找到”键是一个不同的野兽)。想想如果key 变量不是全局变量,您将如何更新它。我将把它留作练习。

    【讨论】:

    • 不知道全局函数。完美的解决方案,非常感谢您,先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2019-04-02
    • 2013-04-09
    相关资源
    最近更新 更多