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

Νικόλαος Κούρας support at superhost.gr
Wed Jun 12 04:54:25 EDT 2013


On 12/6/2013 11:27 πμ, Denis McMahon wrote:
> On Tue, 11 Jun 2013 13:20:52 -0700, Νικόλαος Κούρας wrote:
>
>> The above if structure works correctly *only* if the user sumbits by
>> form:
>>
>> name, month, year or month, year
>>
>> If, he just enter a year in the form and sumbit then, i get no error,
>> but no results displayed back.
>>
>> Any ideas as to why this might happen?
>
> Yes, I know exactly why this is happening.
>
> It is for one of two reasons. You may determine which one using the
> following secret code which I stole from the illuminati in 1836:
>
> import random
>
> reason = { 1: "Input data is not as expected by coder", 2: "Flow control
> logic not performing as coder expects", 3: "Badger Badger Badger Badger
> Badger Badger Badger Badger Mushroom", 4: "Please try again", 5:
> "Snaaaaake", 6: "Grumpy cat says fix your own damn code", 7: "I'll be
> back" }
>
> print reason[ random.choice( reason.keys() ) ]
>
> Note - this only has a small chance of actually doing what you want (ie
> giving a possibly accurate answer), but it sounds as if that's a level of
> precision you're used to working with anyway.
>
> On a slightly more serious note, if you can't apply yourself to debugging
> a case of "the program logic isn't doing what I expect" for some value of
> program logic that you coded, that may be a hint that:
>
> a) you don't actually understand what the program logic is doing
>
> b) you shouldn't be writing logic so complex that you can't see how to
> debug it
>
> c) your logic is overly convoluted and complex
>
> d) all of the above
>
> So perhaps you need to scrub the logic altogether, and start again taking
> smaller steps.
>
> You could also perhaps do with a lesson in De Morgan's theorem:
>
> not a and not b and not c = not ( a or b or c )
>
> not a or not b or not c = not ( a and b and c )
>
> and sometimes the alternative form is easier to understand
>
> Now, looking at your code here are two important questions:
>
> (1) What is the initial values of name, month and year?
> (2) Which block is actually being executed?
>
> Bear in mind that if a branch other than one you expect to be executed is
> executing, the fail condition might not be what you think it is.
> Specifically, is it possible that the code is executing the wrong branch
> and tripping up when it tries to generate or perhaps execute the sql
> statement empty strings, or just not getting any result from the sql
> because of the empty strings?
>
> Hint - take the code from the current file, apply a liberal smattering of
> print statements, and test it for various values of month, year and name.
>
> def test(name, month, year):
>          print "name, month, year ::", name, month, year
> 	if not re.search( '=', name ) and not re.search( '=', month ) and
> not re.search( '=', year ):
> 		print "branch 1"
> 	elif not re.search( '=', month ) and not re.search( '=', year ):
> 		print "branch 2"
> 	elif not re.search( '=', year ):
> 		print "branch 3"
> 	else:
> 		print "branch 4"
>
> # testing logic for 8 possible input conditions
>
> test("=", "=", "=")
> test("=", "=", "x")
> test("=", "x", "=")
> test("=", "x", "x")
> test("x", "=", "=")
> test("x", "=", "x")
> test("x", "x", "=")
> test("x", "x", "x")
>

Thank you but i already foudn out what the problem was, i just don't 
known how to fix it. Here is is again:


Here is the defines of those variables. as you can see are all tuples

# populate names, months, years
names.add( '==========' )
months = ( '==========', 'Ιανουάριος', 'Φεβρουάριος', 'Μάρτιος',
'Απρίλιος', 'Μάϊος', 'Ιούνιος',
            'Ιούλιος', 'Αύγουστος', 'Σεπτέμβριος', 'Οκτώβριος',
'Νοέμβριος',
'Δεκέμβριος' )
years = ( '==========', 2010, 2011, 2012, 2013 )


========================

i used         print( name, month, year ) and noticed that all values 
returned as expected when selected fro drop-down menus and submitted.

But when it comes to select '==========' from month instead of
'==========' to be submitted a zero gets submitted and i think the 
problem is the way i'm filling up months into the drop down menu which is:


for i, month in enumerate(months):
     print('<option value="%s"> %s </option>' % (i, month) )


the if case does not execute because of the way it checks for None entry
which is: elif '=' not in year:

but if enumerate yields 0 instead of '==========' then elif '=' not in
year of course fails.

So, i must tell:

for i, month in enumerate(months):
     print('<option value="%s"> %s </option>' % (i, month) )

to somehow return '==========' instead of 0 but don't know how.



More information about the Python-list mailing list