omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Variables in functions

    Pythonista
    4
    8
    4824
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Cubbarooney104
      Cubbarooney104 last edited by

      I'm having trouble with variables inside functions (created with "def").
      For example, here is a really simple code I made:

      <pre>def first():
      star = 3
      def second(lego):
      print(lego + 1)
      first()
      second(star)</pre>

      Instead of the program returning the number 4, it says that star is "not defined". I'm sure this is something simple, but I have NO clue.

      Thanks,
      Cubbarooney

      1 Reply Last reply Reply Quote 0
      • jugisto135
        jugisto135 last edited by

        For further information read up on variable scopes and namespaces in the reference or tutorial.

        Simply put, each variable is bound to a certain scope, global or local. Your star is a local variable of and only of the first function. If you want your star variable to be an argument of your second function, declare star globally. So move 'star = 3' to outside of both functions.

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by

          The star variable is only valid <em>within</em> the first function, that's called a <em>local</em> variable. If you want to use values that you have in a function outside of that function, you would typically return the value and then assign that return value to another variable, like this:

          <pre>def first():
          star = 3
          return star

          def second(lego):
          print lego + 1

          foo = first()
          second(foo)</pre>

          An alternative would be to declare star as a global variable before the first function. In order to modify it within a function, you'd then have to use the global keyword.

          <pre>star = 1
          def first():
          global star
          star = 3

          def second(lego):
          print lego + 1

          first()
          second(star)</pre>

          1 Reply Last reply Reply Quote 0
          • Cubbarooney104
            Cubbarooney104 last edited by

            @jugisto and @omz
            Thanks to both of you! :D
            All three ways work, but not for all things.

            Just to make sure I understand, in the following code:
            <pre>spam = 123
            knight = 2
            def rubber():
            gulcup = 6

            def chicken():
            global knight, parrot
            knight = 42
            parrot = knight/2</pre>

            'spam', 'knight', and 'parrot' are global variables while 'gulcup' (pronounced gill-cup, incase you don't get the reference) is a local variable to the function 'rubber()'.

            Do note that I do not define parrot until the function 'chicken()'. Based on my experimentation this is fine. If I'm wrong about this (or anything above really) let me know.

            Again, thanks for the help (and speedy response. I wish I was that fast)

            Cubbarooney

            Edit: Also, how do you get the code into those yellow boxes? Are my quotation marks interfering? I took them out now just in case. Take that back, I just noticed that (when I pressed edit) that the quotation marks changed to html

            1 Reply Last reply Reply Quote 0
            • omz
              omz last edited by

              You're correct, though I generally wouldn't recommend defining a global variable inside of a function, that can get quite confusing.

              1 Reply Last reply Reply Quote 0
              • Cubbarooney104
                Cubbarooney104 last edited by

                Hmmm...
                Every time I try doing that it doesn't work.

                Here is some code I just tried:

                <pre>global red
                blue = input()
                def color():
                red = blue + 2
                color()
                print('red', red,)</pre>

                An error pops up saying "global name 'red' not defined"

                Thanks,
                Cubbarooney

                1 Reply Last reply Reply Quote 0
                • Dalorbi39
                  Dalorbi39 last edited by

                  you need to tell the function color, that you're referring to the global red

                  <pre>
                  global red
                  blue = input()
                  def color():
                  global red
                  red = blue + 2
                  color()
                  print('red', red,)
                  </pre>

                  1 Reply Last reply Reply Quote 0
                  • Cubbarooney104
                    Cubbarooney104 last edited by

                    Lol, ok. So make it exist on the "outside" and then (when used in a defined function) tell the program to use the global variable.

                    Thanks for all your help!
                    Cubbarooney

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors