Please, I just need to improve my dictionary project in python
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2356408/problems-with-nested-loops">Problems with nested loops…</a>  </p>
</blockquote>



<p>I have 2 different dictionary programs that I want combined (or put together).<br>
The first one has the possibility to get good translation with grammar etc.<br>
Here is the code so far:</p>

<pre><code>words = {('i',): 'jeg', ('read',): 'leste', ('the', 'book'): 'boka'}
max_group = len(max(words))
sentence = "I read the book".lower().split()
translation = []
position = 0

while sentence:
    for m in range(max_group - 1, -1, -1):
        piece = tuple(sentence[:position + m])
        if piece in words:
            translation.append(words[piece])
            sentence = sentence[position + m:]
    position += 1

print(' '.join(translation))
</code></pre>

<p>In program 2 you can write more than just one sentence at a time.<br>
You will also get the first letter in a sentence capitalized.<br>
The third thing is that you get the word that isn’t in the dictionary printed out. </p>

<p>Here is the code for program 2:</p>

<pre><code>import re
words = {'i':'jeg','am':'er','happy':'glad'}

sentence =input('Write a sentence:')
sentence = sentence.split()
translation = []
for word in sentence:
    piece = re.sub('[^a-z0-9]', '', word.lower())
    punctuation = word[-1] if word[-1].lower() != piece[-1] else ''
    if piece in words:
        translation.append(words[piece] + punctuation)
    else:
        translation.append(word)
translation = ' '.join(translation).split('. ')
print('. '.join(s.capitalize() for s in translation))
</code></pre>

<p>So the question is: How could I use the best of each program
and just make a good one?...</p>

<p>I appreciate every good effort…<br>
Thanx in advance!</p>
