title: dfa implementation in python for readling web addresses
body: <p>recently I made a python implementation of a fairly simple DFA that's intended to check web addresses, and now I have a program that compiles and runs successfully! My only issue now is bad output. Here is my current code:</p>
  
  <pre><code>from collections import defaultdict
  dfa = {'initial': defaultdict(lambda: 'invalid', w='w'), 'w': defaultdict(lambda: 'invalid', w='ww'), 'ww': defaultdict(lambda: 'invalid', w='www'), 'www': defaultdict(lambda: 'invalid', [('.','www.')]), 'www.': defaultdict(lambda: 'www.*', [('.','invalid')]), 'www.*': defaultdict(lambda: 'www.*', [('.','www.*.')]), 'www.*.': defaultdict(lambda: 'www.*', [('c','www.*.c')]),'www.*.c': defaultdict(lambda: 'www.*.c', [('o','www.*.co')]), 'www.*.c': defaultdict(lambda: 'www.*.c', [('a','www.*.ca')]), 'www.*.co': defaultdict(lambda: 'www.*.co', [('m','www.*.com')])}
  cases=["www.commercial.com", "www.co.com", "www.x.com", "njit.ca", "www.stanford.com", "www.com.com", "ww.com.com", "co.ca", "www.ca.co.ca", "www.com", "www.ca", "www.cat.com", "wwwb.co.ca", "www..com", "www.abcd.co", "www.company.co.ca", "company.co.ca", "www.co.co.ca", "www.com.co.ca", "www.co.ca"]
  
  
  s = input("Would you like to input a string? y/n ")
  if(s == 'n'):
      exit
  elif(s == 'y'):
      def accepts(transitions, initial, accept, s):
          state = initial
          for char in s:
              state = transitions[state][char]
          if(state == 'invalid'):
              return 1
      return state in accept
  for case in cases:
      if(accepts(dfa,'initial',['.com','.co','ca'],case)==1):
          print(case + " is an invalid web address!")
      else:
          print(case + " is a valid web address!")
  </code></pre>
  
  <p>Running the program for the test case, 'www.hi.com' exits and returns "invalid url". Can anyone tell me what I'm doing wrong?</p>
  
  <p>Running this will execute fine until it reaches this part of the DFA:</p>
  
  <pre><code> 'www.': defaultdict(lambda: 'www.*', [('.','invalid')])
  </code></pre>
  
  <p>Note I had to use a tuple to handle for a dot as a key, since it isn't accepted. It is here to test for this case:     "www.."</p>