title: Python: "gibberish sentence generator", misbehaving in a weird manner
body: <p>I'm trying to create a simple &quot;gibberish generator&quot; program in Python that prints a string of random gibberish consisting of characters, spaces and a punctuation mark at the end (a complete sentence in other words). It's basically working already, but I've run into a weird problem that I just cant get my head around.</p>
  <p>Somehow the last &quot;word&quot; in my gibberish string is always getting longer than it should be, despite my code explicitly restricting any of the words getting longer than 11 characters. After going through the code for god knows how many times I still don't get what could possibly be causing this. Funny thing is, it only becomes truly noticeable with long strings, while short sentences (up to 50 chars) look mostly fine.</p>
  <p>These are two example outputs I'm getting when I run it in Windows powershell:</p>
  <p>first with 50 chars:</p>
  <blockquote>
  <p>How many gibberish characters would you like to print out? 50</p>
  <p>Uxlouasieyt uoygigjas eayouiumza gyfejmu th egkyaulheeb.</p>
  </blockquote>
  <p>second with 300 chars:</p>
  <blockquote>
  <p>How many gibberish characters would you like to print out? 300</p>
  <p>Yhiaztexj ekkexe iiuiyx itozlyui zao cegyeuyiml aofzyyreet cofi owzycwobla rreyblioca rla tpocnelavj ytpa
  x eefra gnyoe yfxyhnivme miert ywy ykhi ee gup eui ttuoi oeoyaf uenyecb apluo yli xmy uiyaoneewe jyxymxal
  y dzaiglu uo eqkiyeiz ke oxayuiayzf yyi iqoezu ekuioyotly viyslaybiiwvymitoeagrejvavihigpyoxawefunodgu!</p>
  </blockquote>
  <p>Notice how the last word in the sentence is getting progressively longer with longer strings, while all the precluding words stay within 11 characters. It's as if the part of the code where the spaces are added in the gibberish_list is being ignored after some point. But why?</p>
  <p>Here is the code in full:</p>
  <pre><code>import random
  
  def gibberishgen():
      alphabet_vowels = ['a','e','i','o','u','y',]
      alphabet_consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']
      gibberish_list = []
      
      while True:
          gibberishamount = raw_input(&quot;How many gibberish characters would you like to print out? &quot;)
          if gibberishamount.isdigit():
              break
          else:
              print &quot;Please give me a number!&quot;
      
      # fill the gibberish_list with characters
      lasttwochars = ['','']
      for char in range(1, int(gibberishamount)+1):
          nextcharvowel = random.choice(alphabet_vowels)
          nextcharconsonant = random.choice(alphabet_consonants)
          if lasttwochars[0] in alphabet_consonants and lasttwochars[1] in alphabet_consonants:   # because I don't want more than 2 consonants in a row
              nextchar = nextcharvowel
          else:
              roll = random.randint(1,10)
              if roll &gt; 5:
                  nextchar = nextcharvowel
              else:   
                  nextchar = nextcharconsonant
          gibberish_list.append(nextchar)
          lasttwochars.append(nextchar)
          lasttwochars.pop(0)
      
      # insert spaces at randomized intervals to separate the &quot;words&quot; from each other
      last_whitespace = 0
      for index in range(0, len(gibberish_list)+1):
          randspace = random.randint(1,10)
          if index &gt;= last_whitespace + 3 and randspace &lt;= 2:     # make sure words don't get too short on average
              gibberish_list.insert(index, ' ')
              last_whitespace = index
          elif index &gt; last_whitespace + 10:                      # ...or too long
              gibberish_list.insert(index, ' ')
              last_whitespace = index
      
      punctlist = ['.', '!', '?']
      
      gibberishstring = ''.join(gibberish_list)
      finalstring = gibberishstring.capitalize() + random.choice(punctlist)
      print &quot;\n&quot;, finalstring, &quot;\n&quot;
      
  gibberishgen()
  </code></pre>
  <p>I would greatly appreciate if someone explained to me what is going on in here. I'm only two months into learning python so yeah, It's very much possible I missed something that should be obvious.</p>
  <p>Also feel free to point out any bad syntax or practice you spot.</p>