Questions
Signature Writing
The following questions will have you practice signature writing.
Write the signature for a function called
name_eval
that takes as input someone’s name as a string and returnsTrue
if the name has an even amount of letters andFalse
if it has an odd amount of letters.Write the signature for a function called
name_size
that takes as input someone’s name as a string and returns the length of their name as an integer.Write the signature for a function called
gcd
that takes two floats as input and returns the integer that is their greatest common divisor.
Complete the function definition
- Line 2 belongs within the body of a function. Add a line of code that will complete the entire function definition.
return x * 2
print(double(x=3))
- Line 2 belongs within the body of a function. Add a line of code that will complete the entire function definition. The function’s name is
my_string
.
return "This is my word, " + word
- Line 2 belongs within the body of a function. Add a line of code to complete the entire function definition. The function’s name is
my_num
andword
is afloat
.
print("This is my number, " + str(word))
Solutions
Signature Writing Solutions
def name_eval(id: str) -> bool:
(Note that I chose to call the parameter id
, but you could’ve chosen any valid name!)
def name_size(id: str) -> int:
(Note that I chose to call the parameter id
, but you could’ve chosen any valid name!)
def gcd(number_a: float, number_b: float) -> int:
(Note that I chose to call the parameters number_a
and number_b
, but you could’ve chosen any valid name!)
Complete the function definition
- Complete function definition
def double(x: int) -> int: # Added this line
return x * 2
print(double(x=3))
- Complete function definition
def my_string(word: str) -> str: # Added this line
return "This is my word, " + word
- Complete function definition
def my_num(word: float) -> None: # Added this line
print("This is my number, " + str(word))