Product


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/3?curriculum_id=4f89dab3d788890003000096#


Oops, try again. Your code looks a bit off--it threw a "invalid literal for int() with base 10: '['" error. Check the Hint if you need help!


the test result is correct


def product(y):
    result=1
    y=str(y)
    for i in y:
        i=int(i)
        result*=i
    print result
    return result

product(1234)


thanks in advance!


#2

the function should handle lists:

def product(y):
    result=1
    y=str(y)
    for i in y:
        i=int(i)
        result*=i
    print result
    return result

product([1,2,3,4])

not integers, then you get an error


#3

Thank you,
If I remove the"product(1234)" or change it to "product([1,2,3,4]), then submit and run, the error message doesn't change, it shows:
Oops, try again. Your code looks a bit off--it threw a "invalid literal for int() with base 10: '['" error. Check the Hint if you need help!


#4

yes, that is what i am telling you. YOu made your code work for integer, you should make it work for list


#5

oh...got it~
Thank you!


#6

the code stetim94 posted was just a hint, not the solution.
It's not done with simply changing the function-call and providing a list of integers instead of an integer.

but you are close to the solution.
lets take a look what you want to do:

  1. you create a local variable called result with value 1 -> correct,
  2. you want to iterate over all integers in the list and multiply each integer with the value stored in result -> correct
  3. you return the result -> correct

so whats wrong? its your conversion to string and back to integer. its not necessary, since you already have a list of integers.
you get the error-message, because you try to convert "[" back to an int, which is not possible :smiley:

y=str(y)   #if the argument was [1,2,3,4], then y now is "[1,2,3,4]", and the first element of your iteration is "["

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.