0
0
Pythonprogramming~10 mins

Polymorphism through functions in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that prints the length of any input.

Python
def print_length([1]):
    print(len(item))

print_length("hello")
Drag options to blanks, or click blank then click option'
Avalue
Blength
Citem
Dobj
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the variable inside the function causes a NameError.
2fill in blank
medium

Complete the code to call the function with a list argument.

Python
def print_length(item):
    print(len(item))

print_length([1])
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
B5
C"hello"
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer like 5 causes a TypeError because integers have no length.
3fill in blank
hard

Fix the error in the function to handle any input and print its length.

Python
def print_length(item):
    print([1](item))

print_length(12345)
Drag options to blanks, or click blank then click option'
Acount
Blen
Csize
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined functions like length or size causes NameError.
4fill in blank
hard

Fill both blanks to create a function that works with strings and lists, printing their lengths.

Python
def print_length([1]):
    print(len([2]))

print_length("test")
print_length([1, 2, 3])
Drag options to blanks, or click blank then click option'
Adata
Citem
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names causes NameError.
5fill in blank
hard

Fill all three blanks to create a function that prints the length of the uppercase version of a string argument.

Python
def print_upper_length([1]):
    upper_str = [2].upper()
    print(len([3]))

print_upper_length("hello")
Drag options to blanks, or click blank then click option'
Atext
Cupper_str
Dtext.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using text.upper() directly in print causes error because it's not stored.
Mismatching variable names causes NameError.