Home > Blockchain >  Python - How do I get the defined variable from one function to another function?
Python - How do I get the defined variable from one function to another function?

Time:08-13

I have simplified it to this:

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()

I had previously defined gameinput in the 'hang()' it collects the user input and so on. My problem is in the 'hangman()' I need gameinput again to make the variable word (its made so a user can either make a word (two_player_word) or one player when it generates a random word (one_player_word()

It works perfectly without gameinput being in a function but after the player either wins or loses I want it to let the user decide if they want to change gamemode or not as shown in main().

There is a lot more code but figured it would be easier to try figure out the problem using just this.

CodePudding user response:

Just pass the gameinput from hang into hangman as an argument.

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")

CodePudding user response:

There are two problems that cause your code to fail (besides indentation).

  1. You are defining nested functions (one/two_player_word) which you then try to call from outside the function (where they are not defined). You can change this by defining the functions outside the function hang().
  2. hangman() uses the gameinput variable but it's not defined nor provided. You can change this by adding it as a parameter to the function call.

Your adjusted code could work like this:

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()
  • Related