list function returns wrong result?

This is the place for queries that don't fit in any of the other categories.

list function returns wrong result?

Postby nsadeh » Sat Apr 05, 2014 11:52 pm

This code takes a list of integers, with the list not bigger than 1000 items. The first items denotes the numbers of entries to follow it, and is not used in the calculation. The goal is to take each entry and return the product of all the other entries in its place. The current code simply returns none. I'm a self-teaching new coder - not a professional developer.

Code: Select all
def product_lst(lst):
    for item in lst:
        if item in lst==int is False:
            return "Sorry, all items in input must be integers"
        elif len_items>1000:
            return "Sorry, please limit input to 1000 or fewer numbers"
            for item in range(lst[1], len_items):
                for item in range(lst[1], len_items):
                return product
print product_lst([4,5,2,2,3])

Should return [12, 30, 30, 20]
Last edited by Yoriz on Sun Apr 06, 2014 12:02 am, edited 2 times in total.
Reason: First post lock, Changed title.
Posts: 1
Joined: Sat Apr 05, 2014 11:47 pm

Re: list function returns wrong result?

Postby micseydel » Sun Apr 06, 2014 1:54 am

There's a lot of problems here and you need to start smaller. The notable things you need to figure out are that this
Code: Select all
item in lst==int is False

doesn't do what you expect, and if you have a return in a loop, if that return statement isn't protected by a condition of some kind then you'll only get one iteration of that loop. For that bit of code I mentioned above... I think you would best learn what's going on there by testing it in an interpreter. Assign item and lst to values and then see what the expression evaluates too. In fact, you should figure out which parts are executed first and look at subexpressions. I'll give you a few hints -- this is how it looks to Python
Code: Select all
((item in lst) == int) is False

and you probably won't want to use is and should consider the types here when these expressions interact with each other.
Join the #python-forum IRC channel on!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
Posts: 1833
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests