[Tutor] New to programming question (Ben M.) (Joseph Q.)
joe_schmoe
geek_show at dsl.pipex.com
Thu Apr 14 19:22:03 CEST 2005
Joseph Quigley wrote:
> >>>>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
>
> Oh, ok. Sorry, my bad :)
> So there is a special use for ==. I must look into this.
> Thanks,
> Joe
>
"=" is for assignment of a value to a variable, as in a = 33
"==" is equivalent to, as in 2 * 2 == 4
I've been caught out on more than a few occasions on this distinction!! :)
HtH
/j
More information about the Tutor
mailing list