Shniper
Shniper

Reputation: 854

Python: Getting value of a variable defined in a function, outside of that function

I need a way to get the value of a variable that was defined in a function outside of that function. It's not necessary but would make my code so much easier to follow. Haven't found any solid answers to this yet.

def pizzais():
  pizza = "yummy"

pizzais()
print(pizza)

this will return an error saying that pizza is not defined. Is there any hack to get around this.

Too better understand my situation here is my code I'm applying it too.

def questions():
    user = input("What is your username?") #username
    race = input("What is your race? (orc, human, elf, goblin)") #race

#won't move on if the player fills an answer that is not an option
    if race == "orc" or race == "human" or race == "elf" or race == "goblin":
        pClass = input("What is your class? (archer, warrior, rogue or mage)")


    else:
        while race != "orc" and race != "human" and race != "goblin" and race != "elf":
            race = input("What is your race? (orc, human, elf, goblin)")
            if race == "orc" or race == "human" or race == "elf" or race == "goblin":
                pClass = input("What is your class? (archer, warrior, rogue or mage)")

#won't move on if the player fills an answer that is not an option               
    if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage":
            correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")

    else:
        while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
            pClass = input("What is your class? (archer, warrior, rogue or mage)")
            if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage":
                correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")

    def correct_def():
        correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
        if correct == "yes": #if player likes their choices the game will begin
            print("Enjoy the game " + user + "!")

        elif correct == "no": #if player doesn't like their choices all questions are asked again
            reAsk = input("What would you like to change?(username, race, class or all)")

        else:
            while correct != "yes" and correct != "no":
                correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
            if correct == "yes":
                print("Enjoy the game " + user + "!")

            elif correct == "no":
                questions()

    if correct == "yes": #if player likes their choices the game will begin
        print("Enjoy the game " + user + "!")

    elif correct == "no": #if player doesn't like their choices all questions are asked again
        reAsk = input("What would you like to change?(username, race, class or all)")

        if reAsk == "username":
            user = input("What is your username?")
            correct_def()

        elif reAsk == "race":
            race = input("What is your race? (orc, human, elf, goblin)")
            while race != "orc" and race != "human" and race != "goblin" and race != "elf":
                race = input("What is your race? (orc, human, elf, goblin)")
            correct_def()

        elif reAsk == "class":
            pClass = input("What is your class? (archer, warrior, rogue or mage)")
            while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
                pClass = input("What is your class? (archer, warrior, rogue or mage)")
            correct_def()

        elif reAsk == "all":
            questions()

        else:
            while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all":
                reAsk = input("What would you like to change?(username, race, class or all)")
                if reAsk == "username":
                    user = input("What is your username?")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "race":
                    race = input("What is your race? (orc, human, elf, goblin)")
                    while race != "orc" and race != "human" and race != "goblin" and race != "elf":
                        race = input("What is your race? (orc, human, elf, goblin)")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "class":
                    pClass = input("What is your class? (archer, warrior, rogue or mage)")
                    while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
                        pClass = input("What is your class? (archer, warrior, rogue or mage)")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "all":
                    questions()

#won't move on if the player fills an answer that is not an option 
    else:
        while correct != "yes" and correct != "no":
            correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
        if correct == "yes":
            print("Enjoy the game " + user + "!")

        elif correct == "no":
            reAsk = input("What would you like to change?(username, race, class or all)")

        if reAsk == "username":
            user = input("What is your username?")
            correct_def()

        elif reAsk == "race":
            race = input("What is your race? (orc, human, elf, goblin)")
            while race != "orc" and race != "human" and race != "goblin" and race != "elf":
                race = input("What is your race? (orc, human, elf, goblin)")
            correct_def()

        elif reAsk == "class":
            pClass = input("What is your class? (archer, warrior, rogue or mage)")
            while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
                pClass = input("What is your class? (archer, warrior, rogue or mage)")
            correct_def()

        elif reAsk == "all":
            questions()

        else:
            while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all":
                reAsk = input("What would you like to change?(username, race, class or all)")
                if reAsk == "username":
                    user = input("What is your username?")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "race":
                    race = input("What is your race? (orc, human, elf, goblin)")
                    while race != "orc" and race != "human" and race != "goblin" and race != "elf":
                        race = input("What is your race? (orc, human, elf, goblin)")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "class":
                    pClass = input("What is your class? (archer, warrior, rogue or mage)")
                    while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
                        pClass = input("What is your class? (archer, warrior, rogue or mage)")
                    print("Enjoy the game " + user + "!")

                elif reAsk == "all":
                    questions()
questions()

Upvotes: 1

Views: 1608

Answers (4)

Keatinge
Keatinge

Reputation: 4341

Two ways to do this, one recommended and one not so much. You should not mess around with global variables unless you know what you're doing. What you are trying to do should 100% use a return. But to be inclusive I put it in because it could theoretically accomplish what you're trying to do.

Best Practice: Using A Return

def pizzais():
  return "yummy"

pizza = pizzais()
print(pizza)

Bad Idea: Using Global Variables

pizza = ""

def pizzais():
  global pizza
  pizza = "yummy"


pizzais()

print(pizza)

Returning Multiple Variables

def get_three_variables():
    var1 = "text1"
    var2 = "text2"
    var3 = "text3"
    return (var1, var2, var3)


response = get_three_variables()
print(response)
#this prints: ('text1', 'text2', 'text3')

print(response[0])
#this prints text1

print(response[1])
#this prints text2

Upvotes: 2

tzaman
tzaman

Reputation: 47780

You should probably read through the Tutorial section on functions - this is just about the most basic thing you use functions for.

Your code sample is a giant mess, but here's a quick example of what you seem to be trying to do:

def ask_race():
    race = None
    options = ['orc', 'human', 'elf', 'goblin']
    while race not in options:
        race = input('What is your race? ({})'.format(','.join(options)))
    return race

Repeat for your other options, then you can do something like:

def questions():
    race = ask_race()
    character_class = ask_class()

There's much more cleanup that could be done, but this should get you started on a somewhat more sane structure.

Upvotes: 2

C Panda
C Panda

Reputation: 3405

  1. Return multiple values in a tuple return x, y, z and unpack the return value like x, y, z = func().
  2. You can use global variables. While setting values inside a function first declare global x and then x = something. But global variables are best avoided.
  3. If you find yourself changing global state a lot, using a class is more appropriate.
  4. You can use closures to maintain state. But that needs a different design and you should do it if you are comfortable with higher order functions. Closures are in principle lightweight and much faster than classes(Beazly).

Upvotes: 1

Pythonista
Pythonista

Reputation: 11615

You're trying to print pizza but what is pizza? You haven't defined it anywhere except for in your function and you're not returning anything. It's a local variable whereas trying to print pizza outside of the scope of your function is looking for a global variable. You can check out this question for local vs. global variables. You could set pizza = pizzais() and then do print(pizza) if you set return "yummy" at the end of your function. Without using return at the end of your function it will return None by default.

Upvotes: 0

Related Questions