Write a Python program that takes a list of numbers as input from the user, calculates…
- User
Write a Python program that takes a list of numbers as input from the user, calculates the sum of all even numbers in the list, and returns the result. The program should check if the input is valid (i.e., if it's a list of numbers), and if it's not, it should return an error message. The program should also handle exceptions gracefully and return a user-friendly error message in case of any exceptions.
- Assistant
Sure, here's a Python program that takes a list of numbers as input from the user, calculates the sum of all even numbers in the list, and returns the result:
def sum_even_numbers(input_list): try: even_numbers = [num for num in input_list if num % 2 == 0] return sum(even_numbers) except TypeError: return "Error: Invalid input. Please enter a list of numbers." input_str = input("Enter a list of numbers, separated by spaces: ") try: input_list = [int(num) for num in input_str.split()] result = sum_even_numbers(input_list) print("Sum of even numbers in the list:", result) except ValueError: print("Error: Invalid input. Please enter only numbers separated by spaces.") - User
thanks
- Assistant
No problem!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)