Austin Rodgers wrote:
I’m trying to learn by using pyschools.com, and ran across a question I can’t
answer. I tried to google it, but I don’t even know what type of function I’m
looking for. I know I am supposed to modify the list, but I just can’t figure
out how. anyway, here’s the question:
Write a function getSumofLastDigits() that takes in a list of positive numbers and returns the sum of all the last digits in the list.
Examples
>>> getSumofLastDigits([2, 3, 4])
9
>>> getSumofLastDigits([1, 23, 456])
10
how would I go about writing this function?
This question has two parts:
1 Get the last digit of a number.
2 Do #1 for a bunch of numbers and add the results.
Put them together and you get this pseudo code:
def getSumOfLastDigits(list_of_numbers):
for each number in list_of_numbers:
get the last digit
add them together and return the result
Let me give a few hints:
- The sum() function takes a list of numbers and adds them.
- Or, you can do the same by hand with a for-loop:
def my_sum(numbers):
total = 0
for number in numbers:
total += number
return number
- The % operator returns the remainder after division, e.g. 17 % 10 = 7.
Now try to write some code and come back if you have any further
problems. Good luck!
P.S. in future, please try to include a *meaningful* Subject line, not
just blank or "no subject".
--
Steven
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor