Complete the code to check if the input is a non-empty string.
def is_valid_input(user_input): return isinstance(user_input, [1]) and len(user_input) > 0
The function checks if the input is a string and not empty. Using str ensures the input is text.
Complete the code to remove leading and trailing spaces from the input string.
def sanitize_input(user_input): return user_input.[1]()
The strip() method removes spaces from the start and end of a string, cleaning the input.
Fix the error in the code to safely convert input to an integer.
def convert_to_int(user_input): try: return [1](user_input) except ValueError: return None
The int() function converts a string to an integer. The try-except block handles invalid inputs safely.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
words = ['apple', 'cat', 'banana', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension maps each word to its length if the word length is greater than 3.
Fill all three blanks to filter and transform a dictionary with values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = {{ [1]: [2] for k, v in data.items() if v [3] 0 }}The comprehension creates a new dictionary with keys in uppercase and values unchanged, only for values greater than zero.
