python - Why is this string always the highest possible number -


this question has answer here:

if month == 1 or 10:     month1 = 0 if month == 2 or 3 or 11:     month1 = 3 if month == 4 or 7:     month1 = 6 if month == 5:     month1 = 1 if month == 6:     month1 = 4 if month == 8:     month1 = 2 if month == 9 or 12:     month1 = 5 

this code always returns month1 equal 5. i'm quite new @ programming, doing wrong? (i guess involves fact 12 highest number, == means equals right?)

edit: first gave wrong reason why not working. others have pointed out,

if month == 1 or 10:     # ... 

is equivalent to

if (month == 1) or 10:     # ... 

so ... gets executed.

you use

if month in (1, 10):     month1 = 0 

or better

a = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5] month1 = a[month - 1] 

or

d = {1: 0, 2: 3, 3: 3, 4: 6, 5: 1, 6: 4,      7: 6, 8: 2, 9: 5, 10: 0, 11: 3, 12: 5} month1 = d[month] 

instead.

yet way of getting same result use datetime module:

from datetime import datetime month1 = (datetime(2011, month, 1) - datetime(2011, 1, 1)).days % 7 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -