[Tutor] conditionals with slicing not seeming to work

Jeff Younker jeff at drinktomi.com
Mon Jan 28 01:51:06 CET 2008


- Jeff Younker - jeff at drinktomi.com -



On Jan 27, 2008, at 3:01 PM, Rob Stevenson wrote:
> I'm working at a certain website's puzzles using pythonin order to  
> learn the language, but am stuck understanding what is going on in  
> my code (ie why it doesn't do as expected)
>
> 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.
>
> Here's the code...
>
> s 
> = 
> """kKjyaqbooOlNkAddgAazFlgKLjlXDGtlvRBYCYQiRfRIfWIYaLZxLrQzrYzBghYOKAaKgXmUpPkCaMmN
> GlAXokgPsdyUjsiaKSSoeCqMrMbQXeRZqkNeAQpujYJFGfbeceunpFNYjuUPiQVOZPXTKhiwPMLKZEKP
> NoEPPwXtRoVfGYIRyRgZWyJrMjuBQNchjZBNQUwSgIyXniXCMeXRfAcDKxskxYvMyRGyXeSlOFKFItyI
> wgDEIuvHFxRfQhtqLKnJfONtkcnDORkZqbtPplsjjTEIsquhSsQTwNZuPVxaTqDvwMONBfCsNJuJpJHZ
> dCdFLtBQPtFQuCdKOrpndJNUFQIDSbetUKylhSUjcDVtbiQrWMRQhAwGUZyPneCGUjGBBTkLqxLAXXtB
> KfErkDaWMFZZeuqDmXKJEGHyToPUhPphfVhgUZgbIuRAtWnroImpJKqqmEZqeNQCKzhjIkKQHURWLXFw
> PBuijeoTSpsVLaOGuLVjMZXkBvVXwUuHfBihziiavGSYofPNeKsTXruMUumRRPQJzvSzJkKbtSipiqBd 
> """
> h = range(len(s)-9)
> for i in h:
>     j=s[i:i+8]
>     if j[0].islower():
>         if j[1:3].isupper():
>           if j[5:7].isupper():
>               print j
>
> Any help much appreciated!

Your slice indexes are off:

 >>> m = "01234567"
 >>> m[0]
'0'
 >>> m[1:3]
'12'
 >>> m[5:7]
'56'
 >>>

What you want is:

 >>> m[2:4]
'23'
 >>> m[6:8]
'67'

-jeff

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080127/1c83bdd3/attachment.htm 


More information about the Tutor mailing list