Practice Makes Perfect: to_21 error


#1

https://www.codecademy.com/courses/python-beginner-en-KAgt5/2/5?curriculum_id=4f89dab3d788890003000096

Error:

Oops, try again. It looks like your range for to_21 is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] instead of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21].

My result / print:

to 21 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
to odds= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
middle_third = 11
None

My code:

to_21 = range(22)

print "to 21 =", to_21[1::]

odds = to_21[1::2]

print "to odds=", odds

def middle_third(to_21):
    to_21_8_14 = []
    for i in to_21:
        if (i >= 8) and (i <= 14):
            to_21_8_14.append(i)
    middle = float(len(to_21_8_14))/2
    if middle % 2 != 0:
        return to_21_8_14[int(middle - .5)]
    else:
        return (to_21_8_14[int(middle)], to_21_8_14[int(middle-1)])

print "middle_third =", middle_third(to_21)

Someone can help me?

Thanks!


#2

Try looking back to the lessons on how to set a range. The output is telling you that they do not want "0" to be in the range. So how would you keep "0" out of it?

EDIT: This may help http://pythoncentral.io/pythons-range-function-explained/


#3

Thank you for your answer! But now I have another error!

Oops, try again. It looks like your list for middle_third is instead of [8, 9, 10, 11, 12, 13, 14].

My code:

to_21 = range(1,22)

print "to 21 =", to_21

odds = to_21[::2]

print "to odds=", odds

def middle_third(to_21):
    to_21_8_14 = []
    for i in to_21:
        if (i >= 8) and (i <= 14):
            to_21_8_14.append(i)
    middle = float(len(to_21_8_14))/2
    if middle % 2 != 0:
        return to_21_8_14[int(middle - .5)]
    else:
        return (to_21_8_14[int(middle)], to_21_8_14[int(middle-1)])
    
print "middle_third =", middle_third(to_21)

#4

Let's try to simplify the code a little. You can do everything the instructions tell you to do in three lines of code.

The syntax they are trying to get you to use is:
to_21[start:stop:step]

To print JUST odd numbers you use to_21[::2], as that will print out every other number starting at 1, hence why they don't want 0 in the range at all. Since you want to go through the entire to_21 range, you do not need a start or stop, but the two colons have to be there for syntax.

To print a range of numbers from say, the middle third, you could use to_21[start:stop] with the start and stop set to the numbers the instructions say. You do not need a step, because you don't want to skip any numbers.


#5

thank you so much! It was simpler than what I was doing!


#6

to_21 = range(1,22)

print "to 21 =", to_21

odds = to_21[::2]

print "to odds=", odds
a=len(to_21)
b=a/3
c=b+b
middle_third=to_21[b:c]
print middle_third


#7

I think that you misunderstood what you need to do with middle_third thing. The exercise asks Finally, create a third list, middle_third, that's equal to the middle third of to_21, from 8 to 14, inclusive..

You've done a bit of overthinking. Middle_third can be done in one line, in similar way you wrote first and second line. Just use [x:y:z] thing.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.