Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 2nd, 2013, 12:39 PM
 lailamalu
Registered User

Join Date: May 2013
Posts: 8
Time spent in forums: 35 m 48 sec
Reputation 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
June 2nd, 2013, 03:19 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
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()```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : June 2nd, 2013 at 03:26 PM.

#3
June 2nd, 2013, 03:24 PM
 lailamalu
Registered User

Join Date: May 2013
Posts: 8
Time spent in forums: 35 m 48 sec
Reputation Power: 0
thanks.

#4
June 2nd, 2013, 03:25 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
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
>>> ```

#5
June 3rd, 2013, 01:47 AM
 zxq9
Contributing User

Join Date: May 2013
Location: Usually Japan when not on contract
Posts: 240
Time spent in forums: 2 Days 11 h 54 m
Reputation Power: 11
Quote:
 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...)

#6
June 3rd, 2013, 09:01 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Newbie with questions