Mixed-up Code Questions¶
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.
Write 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.
Create a function called bmi
that takes height
(in inches) and weight
(in pounds) as parameters. It should calculate the ‘’bmi’’ by squaring the height then dividing the weight by the height squared and then converting to metric by multiplying by 703.
Watch your indentation!
Write a function called bmi
that takes height
(in inches) and weight
(in pounds) as parameters. It should calculate the ‘’bmi’’ by squaring the height then dividing the weight by the height squared and then converting to metric by multiplying by 703.
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 z
, y
, and z
. For example, square(2)
should return 4
and sum_of_squares(2, 4, 6)
should return 56.
For example, square(2)
should return 4
and sum_of_squares(2, 4, 6)
should return 56.
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 z
, y
, and z
. For example, square(2)
should return 4
and sum_of_squares(2, 4, 6)
should return 56.
Create a function called average(num1, num2)
, which finds the average of two numbers num1
and num2
. For example, average(10,4)
should return 7.0
.
Note: there are two extra code blocks, and watch your indentation!
Write a function called average(num1, num2)
, which finds the average of two numbers num1
and num2
. For example, average(10,4)
should return 7.0
.
Create a function called swapValues(val1, val2)
, which takes two values and swaps them.
It then returns val1. For example, swapValues(8,2)
should return 2
. Note: there is an extra code block, and
watch your indentation!
Write a function called swapValues(val1, val2)
, which takes two values and swaps them. It then returns val1. For example, swapValues(8,2)
should return 2
Create a function called get_avg_drop_lowest(num_list)
which returns the average of a list of numbers, ignoring the lowest value. However, if the list only contains one value, then return that. For example, get_avg_drop_lowest([90])
returns 90
, get_avg_drop_lowest([90, 10])
also returns 90
, and get_avg_drop_lowest([90, 10, 0])
returns 50
.
Write a function called get_avg_drop_lowest(num_list)
which returns the average of a list of numbers, ignoring the lowest value. However, if the list only contains one value, then return that. For example, get_avg_drop_lowest([90])
returns 90
, get_avg_drop_lowest([90, 10])
also returns 90
, and get_avg_drop_lowest([90, 10, 0])
returns 50
.
Create a function called compare(p1, p2)
which returns whichever is greater of its two parameters. Note:
there are two unused code blocks.
Write a function called compare(p1, p2)
which returns whichever is greater of its two parameters.
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!
You are driving a little too fast, and a police officer stops you. Write 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.
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!
Write 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'
.
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 "off"
, and otherwise return "10:00"
. If we are not
on vacation and it is a weekend, it should return "10:00"
, and otherwise return
"7:00"
. Note: there are two extra code blocks, and watch your indentation!
Write 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 "off"
, and otherwise return "10:00"
. If we are not on vacation and it is a weekend, it should return "10:00"
, and otherwise return "7:00"
.
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!
First write a function called square_it
which squares the parameter n
and returns the result. Then, write a function called cube_it
which cubes the parameter n
and returns the result.
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
.
Write 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
.