Skip to main content

Section 6.16 Mixed-up Code Questions

Checkpoint 6.16.1.

Create a function len_str that uses a built-in function to return the number of characters in the passed string str. Then print the result of a call to len_str passing in the string I love Python!. It should return 14.

Checkpoint 6.16.2.

Create a function called distance that takes time (in seconds) as a parameter. It should calculate the distance an object falls (in meters) in the given time with the formula \(d = {1 \over 2}gt^2\text{,}\) where t is the time and g is the gravitational acceleration, which is 9.8 meters per second squared on Earth. Watch your indentation!

Checkpoint 6.16.3.

Write two functions. The first is square(x), which returns x squared. The second function is sum_of_squares(x, y, z), which returns the sum of the squares of three numbers x, y, and z. For example, square(2) should return 4 and sum_of_squares(2, 4, 6) should return 56.

Checkpoint 6.16.4.

Create a function called bigger(p1, p2) which returns whichever is greater of its two parameters. Note: there are two unused code blocks.

Checkpoint 6.16.5.

You are driving a little too fast, and a police officer stops you. Create a function called caught_speeding(speed, is_birthday) which returns the type of ticket the police officer will give you. If speed is 60 or less, the result is "no ticket". If speed is between 61 and 80 inclusive, the result is "minor ticket". If speed is 81 or more, the result is "major ticket". All this is true, unless it is your birthday – on that day, your speed can be 5 higher in all cases. Note: there are two extra code blocks and lots of indentation to watch out for!

Checkpoint 6.16.6.

Create a check_guess(guess, target) function which computes if a guess is too low, too high, or correct compared to the target. Return 'too low' if guess is less than target, 'correct' if they are equal, and 'too high' if guess is greater than target. For example, check_guess(5, 7) returns 'too low', check_guess(7, 7) returns 'correct', and check_guess(9, 7) returns 'too high'. Note: there are three extra code blocks, and watch your indentation!

Checkpoint 6.16.7.

Put the code blocks below to define the function alarm_clock. It will be given a day of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, …6 = Sat, and a boolean indicating if we are on vacation, and will return a string indicating when the alarm clock should ring. If we are on vacation and it is a weekend (0 = Saturday or 6 = Sunday), it should return 0 (indicating “off”), and otherwise return 10 (for 10 a.m.). If we are not on vacation and it is a weekend, it should return 10, and otherwise return 7 (for 7 a.m.). Note: there are two extra code blocks, and watch your indentation!

Checkpoint 6.16.8.

First create a function called square_it which squares the parameter n and returns the result. Then, create a function called cube_it which cubes the parameter n and returns the result. Note : there are three extra code blocks, and watch your indentation!

Checkpoint 6.16.9.

Create a function called distance which returns the distance between two coordinates using the distance formula: d = √((x_2 - x_1)² + (y_2 - y_1)²). Use two functions in Python’s math module (math.pow, math.sqrt). The function math.pow(a,b) returns a raised to the b power. The function math.sqrt(a) returns the square root of a.