Question
Write in python and include explanations and screenshot please Question 1 a). Write a function named positive_odds_sum(numbers) with a list of integers as the parameter.
Write in python and include explanations and screenshot please
Question 1 a). Write a function named positive_odds_sum(numbers) with a list of integers as the parameter. The function must return the sum of all values in the list of integers which are both positive and odd. If the list contains any invalid values, the function should ignore them and continue to do the calculation.
MUST USE the try... except syntax in your solution. For example, if the list contains the following values:
my_list = [1, 2, 3, 4, 'NA', -7, 1]
then the function should return the value of 5.
Test | Result |
my_list = [1, 2, 3, 4, 'NA', -7, 1] print(positive_odds_sum(my_list)) | 5 |
my_list = [1, 2, 3, 4, "two", 2] print(positive_odds_sum(my_list)) | 4 |
print(positive_odds_sum([])) | 0 |
print(positive_odds_sum(['NA'])) | 0 |
b).
Write a function named increment(a_list, index) with a list of numbers and an index position as parameters The function should add 1 to the number in the specified index position.
The function should validate the parameter types. If the parameter(s) is/are invalid, the function should print "ERROR: Invalid input."
If the index is valid but the index is out of the range (e.g. the list has no element at index the parameter index) the function should print "ERROR: Invalid index: n." where n is the index specified in the parameter.
MUST USE the try... except syntax in your solution.
Test | Result |
list1 = [1, 2, 3] increment(list1, 1); print(list1) | [1, 3, 3] |
list1 = [1, 2, 3] increment(list1, 4); print(list1) | ERROR: Invalid index: 4. [1, 2, 3] |
list1 = [1, 2, 3] increment(list1, 'two'); print(list1) | ERROR: Invalid input. [1, 2, 3] |
c).
Write a function named sum(numbers, index1, index2) with a list of values and 2 index positions as parameters The function must return the sum of the two values located at the specified index positions
If the two index positions are within the range, the function should return the sum. Otherwise, the function should return "ERROR: Out of range!". If the calculation could not be done, the function should return "ERROR: Invalid number!". MUST USE the try... except syntax and handle the IndexError in your solution.
Test | Result |
list1 = [10, 25, 39, 42, 78, 93, 24, 56] print(sum(list1, 5, 6)) | 117 |
list1 = [1, 2, 3] print(sum(list1, 2, 3)) | ERROR: Out of range! |
list2 = [1, 'A', 2] print(sum(list2, 1, 2)) | ERROR: Invalid number! |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started