hhtoo-lwin-avatar

multiple choices for python

give me the answer and question for python that can ask in exam. I would like to take a test before exam
0%

3 replies

over 2 years ago
TOPGradeBooster.-avatar
Certainly! Here's a Python programming question along with its answer. This question is suitable for testing your understanding of basic Python concepts. Question: Write a Python function called calculate\_average that takes a list of numbers as an argument and returns the average (mean) of those numbers. If the list is empty, the function should return None. Your function should handle both integer and floating-point numbers. Answer: pythonCopy codedef calculate\_average(numbers): if not numbers: return None total = sum(numbers) average = total / len(numbers) return average Test the function numbers_list = [5, 10, 15, 20, 25]
result = calculate_average(numbers_list)
print(f"The average is: {result}") This question checks your knowledge of function creation, handling edge cases (empty list), and basic arithmetic operations. Feel free to test and modify the code. If you have any questions or need explanations, feel free to ask!
over 2 years ago
JoannaBarradas2902-avatar
def calculate_average(numbers): if not numbers: return None ​ # Validate that all elements are numbers if not all(isinstance(x, (int, float)) for x in numbers): raise ValueError("All elements in the list must be numbers") ​ total = sum(numbers) average = total / len(numbers) return average ​ Test the function numbers_list = [5, 10, 15, 20, 25] result = calculate_average(numbers_list) print(f"The average is: {result}") ​