1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: How to get my variables out of a function

  1. Sep 12, 2008 #1
    1. The problem statement, all variables and given/known data

    The exercise: Make a Python function for computing
    g(t) = e**(-a * t**2) and gderiv(t) = -2*a*t*g(t)

    Return the function values of g(t) and gderiv(t). Apply the function to write out a result in the format:
    g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531


    I have made a function g(t, a) and gderiv(t,a), but I dont know how to print t and a so that i can print the result in the format: "g(1, a=0.5)=0.606531, g’(1, a=0.5)=-0.606531"





    3. The attempt at a solution

    from math import e

    def g(t, a):
    G = e**(-a*t**2)
    Gderiv = -2*a*t*G
    return G, Gderiv,

    G,Gderiv = g(t = 1, a = 0.5)

    print "g(%g, a=%g) = %.6f" % (t, a, G)

    Edit: The problem is that t and a are not defined.
     
  2. jcsd
  3. Sep 12, 2008 #2

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    I've never done any programming in python, but I assume that there is an input function which allows the user of your program to input the values of t and a... I'd try something like this:
    Code (Text):
    from math import e

    def g(t, a):
        G = e**(-a*t**2)
        Gderiv = -2*a*t*G
        return G, Gderiv

    t=input("Please input value of t   ")  
    a=input("Please input value of a   ")

    G,Gderiv = g(t , a )

    print "g(%g, a=%g) =  %.6f" % (t, a, G)
    print "g'(%g, a=%g) =  %.6f" % (t, a, Gderiv)
    And then when you run the program you input the values of t and a when prompted.
     
  4. Sep 12, 2008 #3
    Thank you
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Python: How to get my variables out of a function
Loading...