Python function returning correct result but interpreter returning strange error -
python newbie here, bear me...
i'm attempting utilize poorly written codeacademy python tutorial, wherein exercise challenge to:
write function called digit_sum takes positive integer n input , returns sum of number's digits.
for example: digit_sum(1234) should return 10 1 + 2 + 3 + 4.
(assume number given positive.)
so, attempt solve challenge writing following code:
userinput = raw_input("please enter number string here: ") n = userinput lst = list(n) usernumbers = [] def digit_sum(n): in lst: b = int(i) usernumbers.append(b) numsum = sum(usernumbers) return numsum print "this total: %s" % digit_sum(n)
in console exercise, seems work expected:
please enter number string here: 123 total: 6 none
however, interpreter returns error (despite console seeming work properly):
oops, try again. function fails on digit_sum(434). returns 18 when should return 11.
why in heck error being returned? can elucidate what's happening here?
unfortunately, there's no 1 ask @ codeacademy, have post question other students read, not getting insights there.
you not understand variable scoping or types. that's "problem". (it's not problem, since you're learning.)
what's expected
the question wants provide function digit_sum
accepts number, , returns number. in case accept 434
, return 18
. is, digit_sum(434)
= 18
.
what you're doing
reading keyboard (raw_input
) , printing (print
) not related question.
if remove non-function parts of solution, have:
def digit_sum(n): in lst: b = int(i) usernumbers.append(b) numsum = sum(usernumbers) return numsum
when try submit this, problems become apparent:
lst
not defined.usernumbers
not defined.- you expect string (
str
) , not number (int
)
addressing issues
we have move definition of
usernumbers
,lst
functiondigit_sum
. if set @ "top" level, not reset each time call function, , that's why codecademy isn't getting correct answer.the code use check yours like:
if digit_sum(434) != 18: print "it should have been 18, got %s" % digit_sum(434)
they have many such calls, , each subsequent call not change value of
lst
orusernumbers
.- this masks error:
digit_sum
function accepts string. is, calling (after incorporating fixes #1 above)digit_sum(434)
codecademy does, result in error, since expect calleddigit_sum("434")
.
a possible solution
the following function based on yours, altered advice above:
def digit_sum(n): lst = list(str(n)) usernumbers = [] in lst: b = int(i) usernumbers.append(b) numsum = sum(usernumbers) return numsum
you'll notice other stuff missing: can add in, it's unnecessary. might want use following test while developing:
def digit_sum(n): lst = list(str(n)) usernumbers = [] in lst: b = int(i) usernumbers.append(b) numsum = sum(usernumbers) return numsum print "digit_sum(%s) = %s" % (434, digit_sum(434)) print "digit_sum(%s) = %s" % (123, digit_sum(123)) print "digit_sum(%s) = %s" % (555, digit_sum(555)) print "digit_sum(%s) = %s" % (18, digit_sum(18)) print "digit_sum(%s) = %s" % (1001, digit_sum(1001))
when run it, yields:
digit_sum(434) = 11 digit_sum(123) = 6 digit_sum(555) = 15 digit_sum(18) = 9 digit_sum(1001) = 2
the news these results correct, you're off start! 1 thing you'll learn small, tedious details matter lot in programming.
a small note
it's not easy or obvious. so-called experts started @ zero, , had learn , make mistakes well. don't discouraged! there a lot of disparate concepts have understand know 100% what's happening. nobody gets @ once.
i respect you're trying learn , grow tremendously, fact you're willing ask questions. it's never easy, little burn might getting in brain when try understand of learning feels like. embrace it! ask questions!
good luck learning!
other resources
- if may, recommend zed shaw's learn python hard way quality, free supplement learning journey. it's excellent!
- i've found udacity's free online courses magnificent resource learning lot of different things. specifically, "intro"-level cs courses in python, , worth time.
- peter norvig's essay "teach programming in ten years" considered classic. it's going long journey, enjoy it!
again, luck.
Comments
Post a Comment