don't understand error message

Syver Enstad syver-en+usenet at online.no
Tue Sep 17 07:01:57 EDT 2002


Manuel Hendel <manuel at hendel.net> writes:

> But I already got a new one.
> 
> [....]
> if domainfield in domain:
>     domainlines.append(line)
> [....]
> 
> This should work if the domainfield and domain got the following
> values, shouldn't it?
> 
> domainfield = 1234.ab.cdef.de
> damain = 1234.ab.cdef.de

No I don't think so. It will only work when the conditions are as
described in the error message. I think it would be wise to assume
that your code is at fault first, instead of assuming that the error
is elsewhere. Assume that you've just made a simple mistake first, and
only after having ruled that out, progress to more and more unlikely
error sources, usually ending with blaming the compiler or
interpreter, which should be the last bug source that you consider.

In my understanding *in* works as follows. It takes an
element as the left argument and a collection as the right element. I
is therefore quite natural that you have to have a single character on
the left and a string on the right.


> 
> But I get this error message:
> 
> TypeError: 'in <string>' requires character as left operand

Yes, read the error message. It tells you that what you did is
invalid, and it also tells you what you have to do if you are going to
use this construct. I think what you want is closer to this:

if domain.find(domainfield) != -1:
    # do stuff

>From the python documentation:

find(sub[, start[, end]]) 
Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end). Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found. 





-- 

Vennlig hilsen 

Syver Enstad



More information about the Python-list mailing list