warherolion
warherolion

Reputation: 127

How to move multiple variables from a function into the global scope?

I'm new to python and I'm working on creating a monopoly like game and am working on the settings aspect of the program and I have a function that asks the user what they want to put in for each setting I am then using another function to import those settings to a text file so they can be stored for later use and also have them be used by the program as settings. Here is where my problem comes I anticipate to have around 15 or so set up questions and the only way I can think of bringing them out of the function to be used in another one and also import them into the global scope so they can be used by the program, is to use return is there another way to do this or would I just have to use return for every variable

Thanks,

# Settings file, if user chooses to run setup this is all the setup 
questions
def gameSettingsSetup():
   print("Lets setup")
   numPlayers = int(input("How many real players are playing: "))
   numAIplayers = int(input("How many AI players will be playing?: 
   "))
   AILevel = input("What AI difficulty level would you like?: ") 

# Game settings all the main game settings

# sends over the settings into the text file
def gameSettingsSave():

Upvotes: 2

Views: 717

Answers (3)

J_H
J_H

Reputation: 20460

Don't share variables across methods using global; make them object attributes instead.

class Game:

    def __init__(self):
        self.questions = []
        self.ai_level = 'easy'

Globals lead to coupling across methods that is hard to reason about. Today you can understand the code, but after it grows for a few weeks it will be an entirely different sort of beast.

Also, please follow PEP-8's advice about using snake_case for method and variable names:

    def game_settings_setup(self):
        print("Let's setup.")
        self.num_players = int(...)
        self.num_ai_players = int(...)
        self.ai_level = ...

Do yourself a favor. Run $ pip install flake8, and then run $ flake8 *.py for tips about the code you've written. Follow flake8's advice, make the edits it suggests.

Upvotes: 2

Nathan
Nathan

Reputation: 10306

You could do

return numPlayers, numAIplayers, AILevel

but it might be easier to store all of your settings in a dictionary:

def gameSettingsSetup():
    print("Lets setup")
    settings = {}
    settings["numPlayers"] = int(input("How many real players are playing: "))
    setting["numAIplayers"] = int(input("How many AI players will be playing?: "))
    settings["AILevel"] = input("What AI difficulty level would you like?: ")
    return settings

Then you can pass the settings dictionary to whichever functions need it and also easily save it all to disk.

Upvotes: 1

rdas
rdas

Reputation: 21285

numPlayers = 0
def gameSettingsSetup():
    global numPlayers
    numPlayers = ...

By using global you can pull the global variables into the function and modify them.

Upvotes: 0

Related Questions