#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Newbie with questions


    I am trying to figure out do I put the global variable on the top or just put the number in the code? Am I placing the function that calculates distance in the correct place? Would I normally do the for statement before the function? I need info in the for statement to calculate distance so I am confused about which one do i go to first?
    hints please but you dont have to give me answers as I would prefer to figure it out on my own....
    # setting global variable in the falling distance equation
    # g = 9.8

    # main function
    def main():
    print("This is a program that calculates falling distance by using")
    print("\na well known gravitational formula." )
    print("\nThis will calculate falling distance in meters based on a ")
    print("\nvariable of time from 1 to 10 seconds.")
    # calling the function that will calculate the actual falling distance

    # for statement that establishes a range of numbers that will be put in time
    #print statement that brings data from falling distance function
    #print(distance, "meters")
    for time in range(1, 11):
    print(distance, "meters")
    falling_distance(distance)


    # defining falling_distance
    def falling_distance(time):
    # formula
    distance = .5 * 9.8 * time * time
    return distance

    # call main function
    main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Variables must be defined before they are used---actually all names need to be defined before they're used. g makes for a good global value. Let's make falling distance depend on time. And let's straighten out the reporting.
    Code:
    # setting global variable in the falling distance equation
    g = 9.8
    
    # main function
    def main():
        print("This is a program that calculates falling distance by using")
        print("\na well known gravitational formula." )
        print("\nThis will calculate falling distance in meters based on a ")
        print("\nvariable of time from 1 to 10 seconds.")
        # calling the function that will calculate the actual falling distance
    
        # for statement that establishes a range of numbers that will be put in time
        #print statement that brings data from falling distance function
        #print(distance, "meters")
        print('{:3s} {:>5}'.format('sec','m'))
        for time in range(1, 11): 
            distance = falling_distance(time)
            print('{:3d} {:5.1f}'.format(time,distance))
        
    
    
    # defining falling_distance
    def falling_distance(time):
        # formula
        distance = 0.5 * g * time * time #[edit, **2 removed]
        return distance
                     
    # call main function    
    main()
    Last edited by b49P23TIvg; June 2nd, 2013 at 02:26 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    thanks.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Oh, I changed your
    time*time to time**2 thinking it would save a dictionary search and thus be faster. Wow was I wrong! Squaring takes 6 times longer.
    Code:
    >>> timeit.timeit('blort**2','blort=7')
    0.42733001708984375
    >>> timeit.timeit('blort*blort','blort=7')
    0.06127500534057617
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    Originally Posted by b49P23TIvg
    Oh, I changed your
    time*time to time**2 thinking it would save a dictionary search and thus be faster. Wow was I wrong! Squaring takes 6 times longer
    That's unexpected. I assume you're on Python 3? Which build/environment? On SL's build of CPython 2.6 I get
    Code:
    >>> timeit('b**2', 'b=7')
    0.26631617546081543
    >>> timeit('b*b', 'b=7')
    0.16246700286865234
    Which isn't as huge a difference, but its still unexpected that multiplication performance enjoys such a gap over exponentiation! (I just ran that on an ancient netbook processor, so don't consider the raw times significant...)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    The system for timeit adventure was
    $ python3
    Python 3.2.3 (default, Oct 19 2012, 19:53:16)
    [GCC 4.7.2] on linux2

    Python could know that the variable is not an instance of a class that can change value upon look up.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo