0

i have two inputs that i would like to use within another function, is it possible to make them global somehow?

here are the two inputs that i would like to use

def UserInput(): 
     while True:
          if ChooseMove in self.pieces():
              KingRow = int(input("Choose Row: "))
              KingColumn = int(input("Choose Column: "))

here is where i would like to use both variables of the inputs

def KingMoves(self, rows, columns):

    FinalMove = []

    FinalMove.append(((KingRow - 1),(KingColumn)))
    FinalMove.append(((KingRow + 1),(KingColumn))) 
    FinalMove.append(((KingRow),(KingColumn + 1)))

    return FinalMove
hiihihihelloo
  • 99
  • 2
  • 8
  • 1
    Are you aware that your function `UserInput` never returns? If it did, you could just return both user inputs and use them wherever you want. Also, since your function appears to be a class method, you could use instance variables to store the data. – DYZ Mar 17 '18 at 22:24
  • While it may seem desirable to you to have the variables globally available, it is not advisable. Best to return them from the first function and pass them onto the second. – quamrana Mar 17 '18 at 22:25
  • Yes, declare then as `global`, but it would be better if you returned them from the function – cdarke Mar 17 '18 at 22:25
  • Yes, it's possible. Have you tried searching for "global variables in Python"? However, it is better to to avoid mutable global state altogether, and instead, pass and return values into the our functions as necessary. If you find you keep passing the same parameters around between closely related functions, consider using a class. – juanpa.arrivillaga Mar 17 '18 at 22:26
  • @juanpa.arrivillaga The OP appears to be using a class. – DYZ Mar 17 '18 at 22:26
  • i am aware, i am not posting the whole function as it is very long. would i t be possible to return both the inputs? – hiihihihelloo Mar 17 '18 at 22:26
  • Why wouldn't it be possible? Did you try? – DYZ Mar 17 '18 at 22:27
  • 2
    Yes, return a tuple: `return KingRow, KingColumn` then `KingRow, KingColumn = UserInput()`, but get rid of that infinite loop. – cdarke Mar 17 '18 at 22:27
  • @DyZ ah yes, didn't notice. – juanpa.arrivillaga Mar 17 '18 at 22:28
  • 1
    Possible duplicate of [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/how-can-i-return-two-values-from-a-function-in-python) – Patrick Artner Mar 17 '18 at 22:31
  • @DyZ im returning both values and using KingRow, KingColumn = UserInput() as shown above but seem to getiting a error saying UserInput is not defined when i run the program, any idea why? – hiihihihelloo Mar 17 '18 at 23:11

1 Answers1

-3

Use the global keyword.

def UserInput(): 
    global KingRow #Tells python that the variable is global
    global KingColumn

    while True:
         if ChooseMove in self.pieces():
             KingRow = int(input("Choose Row: "))
             KingColumn = int(input("Choose Column: "))

Take note that you only need to say that a variable is global once. Also, global variables are dangerous if misused.If you want, instead you can rewrite your code to return the two variables then pass them as an argument like this:

return KingRow, KingColumn

Then:

row, column = UserInput()
Javier Lim
  • 149
  • 1
  • 14