[Tutor] conditionals with slicing not seeming to work
Ricardo Aráoz
ricaraoz at gmail.com
Mon Jan 28 15:39:07 CET 2008
Alan Gauld wrote:
> "Rob Stevenson" <rob.rstevenson at gmail.com> wrote
>
>> I'm working at a certain website's puzzles using
>> python in order to learn the language,
>
> OK, Then I'll add some other comments
>
>> the intention of this snippet is to only print slices where
>> character 1 is
>> lower case, 2-4 and 6-8 are upper. The logic here looks right to a
>> VB eye.
>
> s="""kKjyaqbooOlNkAddgAazFlgKLjlXDGtlv....
>> etc...
>
>> h = range(len(s)-9)
>> for i in h:
>
> more conventionally in python to just say
>
> for i in range(len(s)-9)
>
>> j=s[i:i+8]
>> if j[0].islower():
>> if j[1:3].isupper():
>> if j[5:7].isupper():
>
> And this would reflect the problem statement better if
> you used boolean logic
>
> if j[0].islower() and j[1:4].isupper() and j[5:8].isupper():
> print j
>
> You could also do this using regular expressions if
> you want an alternative approach.
>
> HTH,
>
Maybe you are trying to pass python challenge #3? Better use re module
as Alan suggested, and remember, it's EXACTLY three on each side.
More information about the Tutor
mailing list