10/17 strange behavior around slices - issue with codeacademy checking?


#1

When I run:

to_one_hundred = range(101)

backwards_by_tens=to_one_hundred[100:0:-10]
print (backwards_by_tens)

I get the error "Oops, try again. to_one_hundred should include 0, but does not." But clearly to_one_hundred does include zero.

If I change the first line to
to_one_hundred = range(0,100)

Then the code passes the section, but the numbers are wrong.

The only thing that fixes the code AND gives the right numbers is to get rid of the explicit ranges in the slice:

to_one_hundred = range(101)

backwards_by_tens=to_one_hundred[::-10]
print (backwards_by_tens)

Seems like a bug in the code checking, no?


#2

Maybe the discussion/opinion will bring some clarity
= http://stackoverflow.com/questions/509211/explain-pythons-slice-notation
and about the =range=
= http://stackoverflow.com/questions/10267817/understanding-range-in-python-for-loop


#3

Maybe my first post was not clear. I am saying that the code academy evaluation is not working properly on this problem. For instance, when I submit:

to_one_hundred = range(0,100)

backwards_by_tens=to_one_hundred[::-10]
print (backwards_by_tens)

print (to_one_hundred)

I get the output:

[99, 89, 79, 69, 59, 49, 39, 29, 19, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
None

But at the bottom it says:
Way to go! Start Next Lesson


#4

to_one_hundred should be read as between-zero-and-100
as the 100 is excluded !!!! in the range-syntax-specification.....


#5

Sorry but I don't think that is what it is. The original code pre-defined to_one_hundred as range(101).

By the way, note that it ALSO accepts

to_one_hundred = range(101)
print (to_one_hundred)

backwards_by_tens=to_one_hundred[::-10]
print (backwards_by_tens)

which yields:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
None

Only one of these last two things I posted should be correct, because they yield different numbers.


#6
backwards_by_tens = to_one_hundred[100::-10]
print backwards_by_tens

Worked for me.


#7

yeah i have had problems with code academy checking in the past as well