A certainl part of an if() structure never gets executed.

MRAB python at mrabarnett.plus.com
Wed Jun 12 13:53:07 EDT 2013


On 12/06/2013 18:13, Νικόλαος Κούρας wrote:
> On 12/6/2013 7:40 μμ, MRAB wrote:
>> On 12/06/2013 12:17, Νικόλαος Κούρας wrote:
>>>
>>>> As with most of your problems you are barking up the wrong tree.
>>>> Why not use the actual value you get from the form to check whether you
>>>> have a valid month?
>>>> Do you understand why "0" is submitted instead of "=========="?
>>>>
>>>> Bye, Andreas
>>>
>>> I have corrected the enumerate loop but it seems thet now the year works
>>> and the selected name nad month fail:
>>>
>>>         if '=' not in ( name and month and year ):
>>>             cur.execute( '''SELECT * FROM works WHERE clientsID =
>>> (SELECT id FROM
>>> clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) =
>>> %s ORDER BY lastvisit ASC''', (name, month, year) )
>>>         elif '=' not in ( month and year ):
>>>             cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit)
>>> = %s and
>>> YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
>>>         elif '=' not in year:
>>>             cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit)
>>> = %s ORDER
>>> BY lastvisit ASC''', year )
>>>         else:
>>>             print( '<h2><font color=red>Πώς να γίνει αναζήτηση αφού
>>> δεν επέλεξες
>>> ούτε πελάτη ούτε μήνα ή τουλάχιστον το έτος?' )
>>>             print( '<meta http-equiv="REFRESH"
>>> content="5;/cgi-bin/pelatologio.py">' )
>>>             sys.exit(0)
>>>
>>>
>>> i tried in , not in and all possible combinations. but somehow it
>>> confuses me.
>>>
>>> doesn't that mean?
>>>
>>>         if '=' not in ( name and month and year ):
>>>
>>> if '=' does not exists as a char inside the name and month and year
>>> variables?
>>>
>>> i think it does, but why it fails then?
>>>
>> You think it does, but you're wrong.
>
> How would you telll in english word what this is doing?
>
> if '=' not in ( name and month and year ):
>
In English, the result of:

     x and y

is basically:

     if bool(x) is false then the result is x, otherwise the result is y

For example:

 >>> bool("")
False
 >>> "" and "world"
''
 >>> bool("Hello")
True
 >>> "Hello" and "world"
'world'

>
> and then what this is doing?
>
> if '=' not in ( name or month or year ):
>
In English, the result of:

     x or y

is basically:

     if bool(x) is true then the result is x, otherwise the result is y

For example:

 >>> bool("")
False
 >>> "" or "world"
'world'
 >>> bool("Hello")
True
 >>> "Hello" or "world"
'Hello'

These can be strung together, so that:

     x and y and z

is equivalent to:

     (x and y) and z

and:

     x or y or z

is equivalent to:

     (x or y) or z

and so on, however many times you wish to do it.

> Never before i used not in with soe many variables in parenthesi, up
> until now i was specified it as not in var 1 and not in var 2 and not in
> var 2 and so on....
>
Keep it simple:

     if '=' not in name and '=' not in month and '=' not in year:

There may be a shorter way, but you seem confused enough as it is.




More information about the Python-list mailing list