#---------------------------------------------------------------------- # Copyright (C) April 24, 2017 -- Dr. William T. Verts # # In the movie "The Avengers" (2012) there is a scene where Tony Stark # (Iron Man) faces off with Loki in Stark Tower. Eventually, Loki # throws Tony out a window, but Tony's Iron Man armor flies out, # catches up, envelopes him, powers up, and activates before he hits # the ground. From the timing, and basic physics (omitting such things # as air resistance), how tall is Stark Tower at minimum? (We don't # know anything about the building height above the current floor.) # # https://www.youtube.com/watch?v=65XnYhk785s # # In the video, Loki tosses Tony out the window at 3:07, and Tony fires # his repulsors to stop his fall just before hitting the ground at # 3:31. He falls for 24 seconds. # # What do we need to know? # (1) Acceleration due to gravity (around 9.8 meters/second-squared) # (2) Basic distance-acceleration-time formula: D = (1/2) A T^2 # # Here is a quick-and-dirty attempt, knowing that we will eventually # need to replace GetATime with a better routine to accept times as # minutes:seconds. #---------------------------------------------------------------------- def GetATime1 (Message): return requestInteger(Message) def Main1 (): Start = GetATime1("Enter Start Time") Stop = GetATime1("Enter Stop Time") Elapsed = Stop - Start Acceleration = 9.8 Distance = 0.5 * Acceleration * Elapsed * Elapsed print "Distance = ", Distance, " meters" return #---------------------------------------------------------------------- # Now, that program may be fine to get a quick answer (2822.4 meters), # but (A) it is not very general, and (B) it doesn't protect against # stupid data entry errors. # # Here is a better version where GetATime can now detect a colon and # from that figure out the minutes and seconds if one is present, or # figure out just seconds if there is no colon. Also, it protects # against the user entering something that is not a correctly formatted # time, and loops until it gets something acceptable. The try-except # structure detects errors that would break the program and lets the # program continue. # # We also introduce a new function to compute the acceleration due # to gravity based on latitude, because the acceleration is different # at the poles from at the equator and at 45 degrees latitude. The # GetAcceleration function computes a tighter value. (New York City, # where Stark Tower is located, is at latitude 40.7128 north.) # # Finally, we compute the distance in both feet and meters. # # Our final program can now handle a wide variety of input values. # We can correct for latitude, we can enter times as seconds or as # minutes:seconds, and illegal values are detected and programmed # around. # # Note that the answers, 2823 meters / 9262 feet are completely # unreasonable for the heights of buildings. First, the tallest # building in the world is only (!) 830 meters / 2722 feet tall, # making Stark Tower taller by a factor of 3.4:1, and nearly two miles # tall. We can tell from the video that this is ridiculous, as there # is a level shot of the Chrysler Building out the window of Stark # Tower; the Chrysler Building is 319 meters / 1046 feet tall. # Hollywood has taken liberties with physics once again! #---------------------------------------------------------------------- import string def GetATime2 (Message): Result = -1 # Force loop to run at least once while Result < 0: try: S = requestString(Message) # Ask the user for the time N = string.find(S, ":") # Value is 0...len(S)-1 if found, -1 if not found if N >= 0: # The colon is present Prefix = S[:N] # Pick off string left of colon Suffix = S[N+1:] # Pick off string right of colon Minutes = int(Prefix) # Attempt to convert, throw error if illegal Seconds = int(Suffix) # Attempt to convert, throw error if illegal else: # The colon is not present Minutes = 0 # No minutes, use entire string as seconds Seconds = int(S) # Attempt to convert, throw error if illegal Result = Minutes * 60 + Seconds except: Result = -1 # Catch any errors, force loop to run once more return Result def GetAcceleration (Latitude): # Latitude is in Degrees AccelPoles = 9.832 Accel45 = 9.806 AccelEquator = 9.780 return Accel45 - 0.5 * (AccelPoles - AccelEquator) * cos(2.0*Latitude*pi/180.0) def Main2(): FeetPerMeter = 3.28084 Acceleration = GetAcceleration(requestNumber("Enter Latitude")) Start = GetATime2("Enter Starting Time") Stop = GetATime2("Enter Ending Time") Elapsed = Stop - Start DistanceMeters = 0.5 * Acceleration * Elapsed * Elapsed DistanceFeet = DistanceMeters * FeetPerMeter print "Height in meters = ", DistanceMeters print "Height in feet = ", DistanceFeet return #---------------------------------------------------------------------- # Notice that the code to be more general and more forgiving of data # entry problems is quite a bit longer than the version that assumes # the user will behave and enter values correctly. This is pretty # typical. It always takes more time and more code to idiot-proof a # program. #----------------------------------------------------------------------