Write a function Sum_3(Lst) in Python, which accepts a list of integers and adds
elements ending with digit 3.
PLZ HELP ME
Answers
Answered by
3
Language:
Python
Program:
def Sum_3(Lst):
sum=0
for i in Lst:
to_str=str(i)
if to_str[-1]=='3':
sum+=i
return sum
Explanation:
- Initialize sum
- I converted each element's datatype (int to string) cause I find it easier to test for last digit when the number of digits in a number is not known.
- Used loop to test each number in the list one by one.
- Added them to sum if the last digit is 3.
- Returned the final sum.
Attachments:
Attachments:
Similar questions