0
0
Pythonprogramming~10 mins

Methods with return values 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 return the sum of two numbers.

Python
def add_numbers(a, b):
    return a [1] b
Drag options to blanks, or click blank then click option'
A-
B/
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use the return statement.
2fill in blank
medium

Complete the code to return the length of the given string.

Python
def string_length(s):
    return [1](s)
Drag options to blanks, or click blank then click option'
Alen
Bsize
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent functions like size() or length().
Trying to use string methods that don't return length.
3fill in blank
hard

Fix the error in the method to return the square of a number.

Python
def square(num):
    result = num [1] num
    return result
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Forgetting to multiply the number by itself.
4fill in blank
hard

Fill both blanks to create a method that returns a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

Python
def word_lengths(words):
    return {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong comparison operator in the condition.
5fill in blank
hard

Fill all three blanks to create a method that returns a dictionary with uppercase keys and values only for items with positive values.

Python
def filter_and_uppercase(data):
    return { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase.
Using wrong comparison operators or filtering conditions.