CodingBat - has22 (Python - Lists2)

preview_player
Показать описание
This is a solution to has22 from the codingbat python list 2 section.
Рекомендации по теме
Комментарии
Автор

I came up with almost the exact same solution but I used "else: return False" and it didn't work >>>
def has22(nums):
for i in range(0, len(nums)-1, 1):
if nums[i] == 2 and nums[i+1] == 2:
return True
else:
return False

Why doesn't this work for all cases?

Edit: I tried moving the "else: return False" back by one indent and it worked. Why does the indent have to line up with the for loop instead of the if statement?

filippians
Автор

A bit of clarification, the range element I believe goes up to but NOT including the second element. So if the len(nums) = 3 it will go up to 2 instead of three. Hence, the len-1 is you accounting for the last element?

Avarn
Автор

hi paul, you explanation is amazing.. I'm new to programming.. can you explain me the range(0, len(num), 1)????
1.range = start to an end.
2.len(nums)= length of the list.
range(0, len(nums), 1)>>> 0 is the starting point, and how does the rest work..???

santoshpaul