Passing variables between functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • America
    New Member
    • Jun 2007
    • 3

    #1

    Passing variables between functions

    I am a first time user, but need some urgent simple help.

    I have information that is local to a function,but I want to use this information in another function called getInput. I will show example below. I have tried placing information in main, but it keeps coming up with nameA not defined.

    nameA and name B appear in the printIntro, and I have obtained these values from raw_input, and they are included in print statements and work fine.

    I wish to include these values in getInput()

    in a print statement
    print nameA "has a probability of", a (a and b are local to the getInputs)
    print nameB "has a probability of", b

    Bartonc or anybody else can you help

    Thanks Jen
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Hi America,

    Welcome to TSDN. I'm sure you will find a wealth of information available in the various forums. These can be found by selecting a link to a drop-down list on the blue menu bar near the top of the screen. Unfortunately, I am not sure of the language you are using and can't redirect this post.

    I hope the experts in the relevant forum can assist with your enquiry.

    Comment

    • America
      New Member
      • Jun 2007
      • 3

      #3
      Hi,

      Thanks for reply.

      I am using Python Programming.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Bartonc will be your man (as you suggested earlier)......
        I will move this to the Python Forum and alert him to it.....

        Comment

        • America
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by DeMan
          Bartonc will be your man (as you suggested earlier)......
          I will move this to the Python Forum and alert him to it.....

          Thanks for your efficiency.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by America
            I am a first time user, but need some urgent simple help.

            I have information that is local to a function,but I want to use this information in another function called getInput. I will show example below. I have tried placing information in main, but it keeps coming up with nameA not defined.

            nameA and name B appear in the printIntro, and I have obtained these values from raw_input, and they are included in print statements and work fine.

            I wish to include these values in getInput()

            in a print statement
            print nameA "has a probability of", a (a and b are local to the getInputs)
            print nameB "has a probability of", b

            Bartonc or anybody else can you help

            Thanks Jen
            I am not sure what you are trying to do because you did not show us much of your code. I would suggest that you try returning values from one function into another function as arguments:[code=Python]def printIntro(a,b) :
            nameA = 'Case 1'
            nameB = 'Case 2'
            print nameA, "has a probability of", a
            print nameB, "has a probability of", b

            def getInput():
            a = 0.75
            b = 0.5
            return a, b

            if __name__ == '__main__':
            printIntro(*get Input())[/code]

            Comment

            Working...