Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'tutorial': 0.03; 'else:': 0.03; 'beginner': 0.05; 'elif': 0.05; 'odd': 0.07; '[1,': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'python': 0.11; 'def': 0.12; 'creates': 0.14; 'comments:': 0.16; 'docs.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sorted()': 0.16; 'sorting': 0.16; 'subject:run': 0.16; 'subject:under': 0.16; 'subject:when': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'trying': 0.19; 'items.': 0.19; 'code,': 0.22; 'header:User- Agent:1': 0.23; 'order.': 0.26; 'suggested': 0.26; 'pass': 0.26; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'code': 0.31; 'assumes': 0.31; 'relies': 0.31; 'figure': 0.32; 'supposed': 0.32; 'says': 0.33; 'comment': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'doing': 0.36; 'charset:us- ascii': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'should': 0.36; 'changing': 0.37; 'list': 0.37; 'list.': 0.37; 'branch': 0.38; 'same.': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'remove': 0.60; 'read': 0.60; 'middle': 0.60; 'new': 0.61; 'numbers': 0.61; 'range': 0.61; 'skip:n 10': 0.64; 'taking': 0.65; 'finish': 0.65; 'reverse': 0.68; 'academy': 0.74; 'subject:have': 0.80; 'william': 0.81; 'float,': 0.84; 'median': 0.84; 'trouble.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Date: Sat, 21 Sep 2013 12:53:38 +0000 (UTC) References: <22b99b0a-598f-4500-9de9-5041c2ce2c8f@googlegroups.com> <24104d3b-ad83-4c15-bb9e-77b61a0bac78@googlegroups.com> <5398c4af-1692-4b7f-9f39-ea392d24199b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.35 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379768043 news.xs4all.nl 15995 [2001:888:2000:d::a6]:54253 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:54553 On 21/9/2013 02:07, William Bryant wrote: In addition to Steven's comments: > > def median(): > medlist = List > medlist.sort() You have just altered the original list. Perhaps sorting it is harmless, but below you actually remove elements from it. One way to avoid that is to use the sorted() method, which creates a new list. > if len(medlist) % 2: This then is the "odd" size branch of code. I think you intended to do the reverse if-test. A comment would be in order. > while len(medlist) > 2: > medlist.popleft() > medlist.popright() At this point, there is exactly one item left in the list. So the code below is going to get into trouble. > if medlist[0] == medlist[1]: > themedian = medlist Is the return value of this function supposed to be a list, or a float? If a float, then you should be doing something like: themedian = medlist[0] > elif medlist[0] != medlist[1]: > #make an if statment to figure out the median if the last 2 left are not the same. Eg [1, 2] the middle value is 1.5 > pass > else: > while len(medlist) > 1: > medlist.popleft() > medlist.popright() > themedian = medlist > return themedian > > It says out of range and i don't know how I am going to make an if statment to figure out the median if the last 2 left are not the same. Eg [1, 2] the middle value is 1.5. Thanks - I am 13 year old beginner in python and i am trying to finish the tutorial on code academy and read the docs. :) Taking Steven's suggested code, and changing it so it uses a COPY of the global list; def median(): # Relies on the global variable called List. # assumes there is at least one number in that list numbers = List.sorted() n = len(numbers) if n % 2 == 1: # Odd number of items. return numbers[n//2] else: a = numbers[n//2 - 1] b = numbers[n//2] return (a + b)/2.0 -- DaveA