Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54553

Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it?

From Dave Angel <davea@davea.name>
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 2013-09-21 12:53 +0000
References (2 earlier) <CAJsdK=JAY8gSxTyMVN+Ac2u_cwmY2PYwrTyQw1i43tPRJ+ymtw@mail.gmail.com> <mailman.168.1379632193.18130.python-list@python.org> <24104d3b-ad83-4c15-bb9e-77b61a0bac78@googlegroups.com> <XnsA241951725F1Cduncanbooth@127.0.0.1> <5398c4af-1692-4b7f-9f39-ea392d24199b@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.225.1379768042.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? William Bryant <gogobebe2@gmail.com> - 2013-09-19 11:46 -0700
  Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? John Gordon <gordon@panix.com> - 2013-09-19 18:51 +0000
    Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? patrick vrijlandt <patrick.vrijlandt@gmail.com> - 2013-09-19 19:08 +0000
  Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Ian Kelly <ian.g.kelly@gmail.com> - 2013-09-19 13:01 -0600
  Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Dave Angel <davea@davea.name> - 2013-09-19 20:18 +0000
  Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Ian Kelly <ian.g.kelly@gmail.com> - 2013-09-19 17:09 -0600
    Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? William Bryant <gogobebe2@gmail.com> - 2013-09-20 00:01 -0700
      Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? William Ray Wing <wrw@mac.com> - 2013-09-20 09:04 -0400
      Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Duncan Booth <duncan.booth@invalid.invalid> - 2013-09-20 13:39 +0000
        Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? William Bryant <gogobebe2@gmail.com> - 2013-09-20 22:59 -0700
        Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? William Bryant <gogobebe2@gmail.com> - 2013-09-20 23:07 -0700
          Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-21 10:25 +0000
          Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Dave Angel <davea@davea.name> - 2013-09-21 12:53 +0000
          Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? MRAB <python@mrabarnett.plus.com> - 2013-09-21 17:18 +0100
          Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? Dave Angel <davea@davea.name> - 2013-09-21 18:09 +0000
      Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it? alex23 <wuwei23@gmail.com> - 2013-09-23 10:39 +1000

csiph-web