Can I store a variable inside of a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ericyan1234
    New Member
    • Jun 2013
    • 6

    Can I store a variable inside of a function?

    I currently have a variable in one of my functions. A separate function after it utilizes the variable but cannot seem to use it and creates a syntax error. Is it possible to store a variable inside of a function?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Can you post some code that demonstrates your issue?

    Comment

    • ericyan1234
      New Member
      • Jun 2013
      • 6

      #3
      My code is a bit long so I'll sum it up here.
      Code:
      def weapon():
         weapon = raw_input("What weapon would you like?")
      def forest_troll():
         return "You stroke out with your " + weapon + "."
      weapon(x)
      forest_troll()
      it results in a syntax error when i try to pull the value of the variable from the previous function.
      Also, the spaces/indents didnt show up sorry
      Last edited by Rabbit; Jun 7 '13, 03:30 PM. Reason: Please use code tags when posting code.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Return the weapon selection in function weapon() similar to:
        Code:
        >>> def weapon():
        ... 	return raw_input("What weapon would you like?")
        ... 
        >>> def forest_troll(w):
        ... 	return "You stroke out with your " + w + "."
        ... 
        >>> forest_troll(weapon())
        'You stroke out with your Knife.'
        >>>
        Your code will indent properly by using code tags.

        Comment

        • ericyan1234
          New Member
          • Jun 2013
          • 6

          #5
          Originally posted by bvdet
          Return the weapon selection in function weapon() similar to:
          Code:
          >>> def weapon():
          ... 	return raw_input("What weapon would you like?")
          ... 
          >>> def forest_troll(w):
          ... 	return "You stroke out with your " + w + "."
          ... 
          >>> forest_troll(weapon())
          'You stroke out with your Knife.'
          >>>
          Your code will indent properly by using code tags.
          I tried your suggested code but it didn't work for me?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            What did not work about it? It does work as you can see. How did you attempt to apply it?

            Comment

            Working...