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?

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):    len_items=lst[0]    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"        else:            for item in range(lst[1], len_items):                product=1                lst.remove(item)                for item in range(lst[1], len_items):                    product=product*item                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.
nsadeh

Posts: 1
Joined: Sat Apr 05, 2014 11:47 pm

Re: list function returns wrong result?

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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
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 6 guests