title: Nested If/Else control flow being executed when it shouldn't be - logical operations error probably
body: <p>I've got this code:</p>
  
  <pre><code>def time24hr(tstr):
  
      if ('a' and ':') in tstr:
          if tstr[:2] == '12':
              tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
              return tstr
  
          elif len(tstr[:tstr.find(':')]) &lt; 2:
          # If length of string's 'tstr' slice (that ends before string ':') is less than 2
              tstr = tstr.replace(tstr[:tstr.find(':')],'0' + tstr[:tstr.find(':')]).replace(':', '').replace('am','hr')
              # Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
              return tstr
  
          else:
              tstr = tstr.replace(':', '').replace('am', 'hr')
              return tstr
  
      elif ('p' and ':') in tstr:
          tstr = tstr.replace(':', '').replace('pm', 'hr')
          return tstr
  
      else:
          return "Time format unknown. Type correct time."
  </code></pre>
  
  <p>When i execute this code <code>print time24hr('12:34am')</code> it returns <code>0034hr</code> string as it should.
  It works also for this <code>print time24hr('2:59am')</code>, returns <code>0259hr</code>. But when i type string with <code>12</code> in it, it automatically omits this part of code <code>if ('a' and ':') in tstr:</code> or this <code>elif ('p' and ':') in tstr:</code> and proceeds to this part:</p>
  
  <pre><code>if tstr[:2] == '12':
      tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
      return tstr
  </code></pre>
  
  <p>So no matter if i type <code>12:15am</code> or <code>12:15pm</code>, this code if finds the <code>12</code> in string, starts doing this above code. <code>print time24hr('12:15pm')</code> returns <code>0015pm</code> but should return <code>0015hr</code> and only for strings with <code>am</code> in it. Else, don't change <code>12</code> to <code>00</code> and return i.e. <code>1244hr</code> for <code>12:44pm</code>.</p>
  
  <p>My question is, why those logical check <code>if ('a' and ':') in tstr:</code> and <code>elif ('p' and ':') in tstr:</code> aren't working?
  This code is meant to be a solution for this quiz -> <a href="http://www.pyschools.com/quiz/view_question/s3-q8" rel="nofollow">http://www.pyschools.com/quiz/view_question/s3-q8</a></p>
  
  <p>==================================================================================</p>
  
  <p>Thanks for helping me, with logical operations.</p>
  
  <p>Also, i've completed above mentioned quiz and here's working code:</p>
  
  <pre><code>def time24hr(tstr):
  
      if (len(tstr[:tstr.find(':')]) == 2) and (tstr[0] == '0'):
          tstr = tstr.replace(tstr[0], '')
  
      if ('a' in tstr) and (':' in tstr):
          if tstr[:2] == '12':
              tstr = tstr.replace('12', '00').replace(':', '').replace('am', 'hr')
              return tstr
  
          elif len(tstr[:tstr.find(':')]) &lt; 2:
          # If length of string's 'tstr' slice (that ends before string ':') is less than 2
              tstr = tstr.replace(tstr[:tstr.find(':')], '0' + tstr[:tstr.find(':')]).replace(':', '').replace('am', 'hr')
              # Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
              return tstr
  
          else:
              tstr = tstr.replace(':', '').replace('am', 'hr')
              return tstr
  
      elif ('p' in tstr) and (':' in tstr):
          if tstr[:2] == '12':
              tstr = tstr.replace(':', '').replace('pm', 'hr')
              return tstr
  
          elif len(tstr[:tstr.find(':')]) &lt; 2:
              PmDict = {'0':'12','1':'13', '2':'14', '3':'15', '4':'16', '5':'17', '6':'18', '7':'19', '8':'20', '9':'21', '10':'22', '11':'23'}
              tstr = tstr.replace(tstr[:tstr.find(':')], PmDict[tstr[:tstr.find(':')]]).replace(':', '').replace('pm', 'hr')
              # Replace every "number" (which is string literally) with it's corresponding "number" in 24HR format, found in 'PmDict' dictionary. Then, as in above cases, remove colon ':' by replacing it with lack of character or space and then replace 'pm' with 'hr'
              return tstr
  
      else:
          return "Time format unknown. Type correct time."
  </code></pre>
  
  <p>I hadn't written this code according to the KISS rule, as you can see - 'cause it's a bit complicated, but works quite well IMO.</p>
  
  <p>It can be tested here -> <a href="http://doc.pyschools.com/console" rel="nofollow">http://doc.pyschools.com/console</a></p>
  
  <p>Cheers everyone and thanks for helping :)</p>