繁体   English   中英

Python - 如何将定义的变量从一个 function 获取到另一个 function?

[英]Python - How do I get the defined variable from one function to another function?

我已将其简化为:

def hang():
p = 1
while p == 1:
    gameinput = input("Please select a gamemode, Type [1] for one player or Type [2] for two player: ")
    if gameinput == "2":
     def two_player_word():
         print("2")
    elif gameinput == "1":
     def one_player_word():
         print("1")


def hangman():
    if gameinput == "2":
        word = two_player_word()
    elif gameinput == "1":
        word = one_player_word()
    print("Word")

def main():
    gamerestart = 1
    while gamerestart == 1:
        print()
        print("Would you like to play again?")
        gameoverinput = input("Press [1] to play again, Press [2] to exit program. ")
        if gameoverinput == "1":
            for i in range(0, 25):
                print()
            hangman()
        elif gameoverinput == "2":
            print("Thank you for playing, goodbye....")
            time.sleep(2)
            quit()
        else:
            print("Invaild option.")

if  __name__ == "__main__":
    main()

我之前在 'hang()' 中定义了游戏输入,它收集用户输入等等。 我的问题在'hangman()'中我需要再次游戏输入来制作变量词(它是这样制作的,因此用户可以在生成随机词时制作一个词(two_player_word)或一个玩家(one_player_word()

如果没有游戏输入在 function 中,它可以完美运行,但是在玩家赢或输之后,我希望它让用户决定是否要更改游戏模式,如 main() 所示。

还有更多代码,但认为仅使用它来尝试找出问题会更容易。

只需将游戏输入从hang hangman gameinput参数。

def hang():
    while True:
        gameinput = input("Please select a gamemode, Type [1] for one player or Type [2] for two player: ")
        if gameinput not in ("1", "2"):
            continue
        hangman(gameinput)
        break


def hangman(gameinput):
    if gameinput == "2":
        word = two_player_word()
    elif gameinput == "1":
        word = one_player_word()
    print("Word")

有两个问题会导致您的代码失败(缩进除外)。

  1. 您正在定义嵌套函数( one/two_player_word ),然后尝试从 function (未定义)外部调用。 您可以通过在 function hang()之外定义函数来更改此设置。
  2. hangman()使用 gameinput 变量,但它没有定义也没有提供。 您可以通过将其作为参数添加到 function 调用来更改此设置。

您调整后的代码可以像这样工作:

import time

def one_player_word():
  print("1")

def two_player_word():
  print("2")

def hang():
  p = 1
  while p == 1:
      gameinput = input("Please select a gamemode, Type [1] for one player or Type [2] for two player: ")
      if gameinput == "2":
        two_player_word()
      elif gameinput == "1":
        one_player_word()

def hangman(gameinput):
    if gameinput == "2":
        word = two_player_word()
    elif gameinput == "1":
        word = one_player_word()
    print("Word")

def main():
    gamerestart = 1
    while gamerestart == 1:
        print()
        print("Would you like to play again?")
        gameoverinput = input("Press [1] to play again, Press [2] to exit program. ")
        if gameoverinput == "1":
            for i in range(0, 25):
                print()
            hangman(gameoverinput)
        elif gameoverinput == "2":
            print("Thank you for playing, goodbye....")
            time.sleep(2)
            quit()
        else:
            print("Invaild option.")

if  __name__ == "__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM