If youve filled in the above code this should just work difference

If youve filled in the above code this should just

This preview shows page 8 - 11 out of 19 pages.

# If you've filled in the above code, this should just work.difference = predicted_distance_m - estimated_distance_mdifferenceOut[18]:0.040223694659304865In [19]:_ = ok.grade('q232')/home/jovyan/materials-x18/materials/x18/lab/1/lab01/tests/q232.py: All tests passed!3. Calling functionsThe most common way to combine or manipulate values in Python is by calling functions. Python comes with many built-in functions that perform common operations.For example, the absfunction takes a single number as its argument and returns the absolute value of that number. The absolute value of a number is its distance from 0 on the number line, so abs(5)is 5 and abs(-5)is also 5.In [20]:abs(5)Out[20]:5In [21]:abs(-5)Out[21]:53.1. Application: Computing walking distancesChunhua is on the corner of 7th Avenue and 42nd Street in Midtown Manhattan, and she wants to knowfar she'd have to walk to get to Gramercy School on the corner of 10th Avenue and 34th Street.
Background image
She can't cut across blocks diagonally, since there are buildings in the way. She has to walk along the sidewalks. Using the map below, she sees she'd have to walk 3 avenues (long blocks) and 8 streets (short blocks). In terms of the given numbers, she computed 3 as the difference between 7 and 10, in absolute value, and 8 similarly.Chunhua also knows that blocks in Manhattan are all about 80m by 274m (avenues are farther apart than streets). So in total, she'd have to walk $(80 \times |42 - 34| + 274 \times |7 - 10|)$ meters to get to the park.Question 3.1.1.Finish the line num_avenues_away = ...in the next cell so that the cell calculates the distance Chunhua must walk and gives it the name manhattan_distance. Everything else has been filled in for you. Use the absfunction.In [22]:# Here's the number of streets away:num_streets_away = abs(42-34)# Compute the number of avenues away in a similar way:num_avenues_away = abs(7-10)street_length_m = 80avenue_length_m = 274# Now we compute the total distance Chunhua must walk.manhattan_distance = street_length_m*num_streets_away + avenue_length_m*num_avenues_away# We've included this line so that you see the distance# you've computed when you run this cell. You don't need# to change it, but you can if you want.manhattan_distanceOut[22]:1462Be sure to run the next cell to test your code.In [23]:_ = ok.grade('q311')/home/jovyan/materials-x18/materials/x18/lab/1/lab01/tests/q311.py: All tests passed!Multiple argumentsSome functions take multiple arguments, separated by commas. For example, the built-in maxfunction returns the maximum argument passed to it.
Background image
In [24]:max(2, -3, 4, -5)Out[24]:44. Understanding nested expressionsFunction calls and arithmetic expressions can themselves contain expressions. You saw an example in the last question:abs(42-34)has 2 number expressions in a subtraction expression in a function call expression. And you probably wrote something like abs(7-10)to compute num_avenues_away.
Background image
Image of page 11

You've reached the end of your free preview.

Want to read all 19 pages?

  • Fall '17
  • Human height

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes