Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

python search numbers in list

i did this excercise at codingbat. it does work, but i am assuming something like a "list out of index" because the end slice part will point to like len(nums)+5

Given an array of ints, return True if .. 1, 2, 3, .. appears in the array somewhere.

array123([1, 1, 2, 3, 1]) → True

array123([1, 1, 2, 4, 1]) → False

array123([1, 1, 2, 1, 2, 3]) → True

def array123(nums):
  wisdom = False

  for num in range(len(nums)+2):
    if nums[num:num+3] == [1, 2, 3]:
      wisdom = True
  return wisdom

2 Answers

Steven Parker
Steven Parker
243,134 Points

I would think you would SUBTRACT two from the range limit, since you can no longer extract 3-item slices once you pass that point.

  for num in range(len(nums) - 2):

Perhaps the "+" sign was just a typo?

thank you for your answer, yes if something -2 makes more sense. tough today i found out slicing is very cool, it never throws out of index.

>>>  str = "h"
>>>  str[:20]
 'h'

its bad to do something like this?