This forum is now read-only. Please use our new forums! Go to forums
Practice Makes Perfect - digit_sum 4/15
Can someone explain why this does not work?
def digit_sum(n):
count = 0
for i in n:
if i >= 0:
return count + i
else:
return "Positive numbers only"
Answer 53fe8a888c1ccc3b820004e5
4 votes
Also solved:
def digit_sum(n):
total = 0
for char in str(n):
total += int(char)
return total
Answer 53fda379282ae3bcce0014e9
0 votes
Ok Solved:
def digit_sum(n):
str_num = str(n)
lst = list(str_num)
count = 0
for i in lst:
count = count + int(i)
return count
Answer 55f279fdd3292f0fcb000705
0 votes
def digit_sum(n): total = 0
for i in str(n):
total += int(i)
return total
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency