struggling with these problems

Discussion in 'Python' started by su29090, Jan 30, 2013.

  1. su29090

    su29090 Guest

    1.Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders .

    I tried this but it didn't work:

    lesser_offenders = worst_offenders[5:6]

    2.Given a variable temps that refers to a list, all of whose elements refer to values of type float , representing temperature data, compute the average temperature and assign it to a variable named avg_temp . Besides temps and avg_temp , you may use two other variables -- k and total .


    I'm not sure about this one but this is what I have:

    for k in range(len(temps)):
    total += temps[k]

    avg_temp = total / len(temps)

    3.Associate the sum of the non-negative values in the list numbers with the variable sum .

    is it this:

    for numbers in sum:
    if sum +=?

    I'm confused at #3 the most

    i'm not doing it in python 3.2.3 it's called Myprogramminglab.
    su29090, Jan 30, 2013
    #1
    1. Advertising

  2. su29090

    MRAB Guest

    On 2013-01-30 03:26, su29090 wrote:
    > 1.Given that worst_offenders has been defined as a list with at least 6 elements, write a statement that defines lesser_offenders to be a new list that contains all the elements from index 5 of worst_offenders and beyond. Do not modify worst_offenders .
    >
    > I tried this but it didn't work:
    >
    > lesser_offenders = worst_offenders[5:6]
    >

    Python uses half-open ranges (and counts from 0), which means that the
    start index is included and the end index is excluded.

    Therefore, worst_offenders[5:6] means the slice from index 5 up to, but
    excluding, index 6; in other words, an empty list.

    The question says "and beyond"; in Python you can just omit the end
    index to indicate everything up to the end.

    > 2.Given a variable temps that refers to a list, all of whose elements refer to values of type float , representing temperature data, compute the average temperature and assign it to a variable named avg_temp . Besides temps and avg_temp , you may use two other variables -- k and total .
    >
    >
    > I'm not sure about this one but this is what I have:
    >
    > for k in range(len(temps)):
    > total += temps[k]
    >
    > avg_temp = total / len(temps)
    >

    You didn't set the initial value of total, which is 0.

    > 3.Associate the sum of the non-negative values in the list numbers with the variable sum .
    >
    > is it this:
    >
    > for numbers in sum:
    > if sum +=?
    >
    > I'm confused at #3 the most
    >

    Well, that's not valid Python.

    What you want to do is to add each number from the list to the sum only
    if it's non-negative, i.e. greater than or equal to 0.

    > i'm not doing it in python 3.2.3 it's called Myprogramminglab.
    >

    Have a look at Dive Into Python:
    http://www.diveintopython.net/
    MRAB, Jan 30, 2013
    #2
    1. Advertising

  3. On Wed, 30 Jan 2013 03:59:32 +0000, MRAB wrote:

    > Python uses half-open ranges (and counts from 0), which means that the
    > start index is included and the end index is excluded.
    >
    > Therefore, worst_offenders[5:6] means the slice from index 5 up to, but
    > excluding, index 6; in other words, an empty list.


    Er, no. It's a one-element list: index 5 is included, index 6 is excluded.

    py> L = list("abcdefgh")
    py> L[5:6]
    ['f']




    --
    Steven
    Steven D'Aprano, Jan 30, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    6
    Views:
    651
    Martin
    Dec 29, 2003
  2. One Handed Man \( OHM#\)

    Struggling With Concept

    One Handed Man \( OHM#\), Jun 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    363
    Jared
    Jun 12, 2004
  3. Guest
    Replies:
    5
    Views:
    390
    Guest
    Dec 26, 2004
  4. John
    Replies:
    1
    Views:
    398
    =?Utf-8?B?RWx0b24gVw==?=
    Oct 20, 2005
  5. Karl Seguin
    Replies:
    0
    Views:
    400
    Karl Seguin
    Dec 8, 2005
Loading...

Share This Page