[Tutor] New to programming question (Ben M.) (Joseph Q.)
Ben Markwell
benmarkwell at gmail.com
Thu Apr 14 04:33:55 CEST 2005
Thanks for everybodys input. Am learning slowly but surely.
Ben
On 4/13/05, jfouhy at paradise.net.nz <jfouhy at paradise.net.nz> wrote:
>
> Quoting Joseph Quigley <cpu.crazy at gmail.com>:
>
> > prefixes = 'JKLMNOPQ'
> > suffix = 'ack'
> >
> > for letter in prefixes:
> > if letter == ('O') or ('Q'):
> > print letter + 'u' + suffix
> > else:
> > print letter + suffix
>
> Hi Joseph,
>
> This still won't work. The reason is that your if statement is interpreted
> like
> this:
>
> if letter == 'O':
> print letter + 'u' + suffix
> elif 'Q':
> print letter + 'u' + suffic
> else:
> print letter + suffix
>
> Do you see? The == "binds more tightly" than the or. And, in python, 'Q'
> is
> considered True for the purposes of tests.
>
> So this is what happens:
>
> >>> prefixes = 'JKLMNOPQ'
> >>> suffix = 'ack'
> >>>
> >>> for letter in prefixes:
> ... if letter == ('O') or ('Q'):
> ... print letter + 'u' + suffix
> ... else:
> ... print letter + suffix
> ...
> Juack
> Kuack
> Luack
> Muack
> Nuack
> Ouack
> Puack
> Quack
> >>>
>
> What you can do instead is this:
>
> for letter in prefixes:
> if letter in ['O', 'Q']:
> print letter + 'u' + suffix
> else:
> print letter + suffix
>
> HTH.
>
> --
> John.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050413/cad0fbc9/attachment-0001.htm
More information about the Tutor
mailing list