title: Finding the word version of a number
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12647254/project-euler-17">Project Euler 17</a>  </p>
  </blockquote>
  
  
  
  <p>So for this problem here of project euler:</p>
  
  <blockquote>
    <p>If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.</p>
    
    <p>If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?</p>
    
    <p>NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.</p>
  </blockquote>
  
  <p>I tried to figure this out myself with no help... but I think I am doing something wrong...</p>
  
  <p>I have this code...</p>
  
  <pre><code>def convert(n):
      if n &lt; 10:
          return placement(n,1)
      elif n == 10:
          return "ten"
      elif n &gt; 10 and n &lt; 20:
          return teen(n)
      elif n &gt;= 20 and n &lt; 100:
          if int(str(n)[1]) == 0:
              return placement(str(n)[0],2)
          else:
              return placement(str(n)[0],2)+convert(int(str(n)[1]))
      elif len(str(n)) == 3:
          x = ""
          h = placement(str(n)[0], 3)
          if int(str(n)[1]) == 0 and int(str(n)[2]) == 0:
              return h
          else:
              z = int(str(n)[1]+"0")+int(str(n)[2])
              x = h + "and" + str(convert(z))
              return x
      elif len(str(n)) == 4:
          x = ""
          t = placement(str(n)[0], 4)
          if int(str(n)[1]) == 0 and int(str(n)[2]) == 0 and int(str(n)[3]) == 0:
              return t
  def teen(n):
      n = int(n)
      if n == 11:
          return "eleven"
      if n == 12:
          return "twelve"
      if n == 13:
          return "thirteen"
      if n == 14:
          return "fourteen"
      if n == 15:
          return "fifteen"
      if n == 16:
          return "sixteen"
      if n == 17:
          return "seventeen"
      if n == 18:
          return "eighteen"
      if n == 19:
          return "nineteen"
      else:
          return ""
  def placement(n,p):
      n = int(n)
      if p == 1:
          if n == 1:
              return "one"
          if n == 2:
              return "two"
          if n == 3:
              return "three"
          if n == 4:
              return "four"
          if n == 5:
              return "five"
          if n == 6:
              return "six"
          if n == 7:
              return "seven"
          if n == 8:
              return "eight"
          if n == 9:
              return "nine"
          else:
              return ""
      if p == 2:
          if n == 1:
              return "ten"
          if n == 2:
              return "twenty"
          if n == 3:
              return "thirty"
          if n == 4:
              return "fourty"
          if n == 5:
              return "fifty"
          if n == 6:
              return "sixty"
          if n == 7:
              return "seventy"
          if n == 8:
              return "eighty"
          if n == 9:
              return "ninety"
          else:
              return ""
      if p == 3:
          if n != 0:
              return placement(n,1) + "hundred"
          else:
              return ""
      if p == 4:
          if n != 0:
              return placement(n,1) + "thousand"
          else:
              return ""
  z = 0
  for x in range(1,1001):
      z += len(convert(x))
  print z
  </code></pre>
  
  <p>This spits out the answer <code>21224</code> but project euler says its wrong, so does anyone see whats wrong with what I am doing? and does anyone have a better way of doing this, if you do can you please explain it instead of just giving me some code?</p>
  
  <p>sorry for this long bit... but if I print each line I get this...</p>
  
  <pre><code>one
  two
  three
  four
  five
  six
  seven
  eight
  nine
  ten
  eleven
  twelve
  thirteen
  fourteen
  fifteen
  sixteen
  seventeen
  eighteen
  nineteen
  twenty
  twentyone
  twentytwo
  twentythree
  twentyfour
  twentyfive
  twentysix
  twentyseven
  twentyeight
  twentynine
  thirty
  thirtyone
  thirtytwo
  thirtythree
  thirtyfour
  thirtyfive
  thirtysix
  thirtyseven
  thirtyeight
  thirtynine
  fourty
  fourtyone
  fourtytwo
  fourtythree
  fourtyfour
  fourtyfive
  fourtysix
  fourtyseven
  fourtyeight
  fourtynine
  fifty
  fiftyone
  fiftytwo
  fiftythree
  fiftyfour
  fiftyfive
  fiftysix
  fiftyseven
  fiftyeight
  fiftynine
  sixty
  sixtyone
  sixtytwo
  sixtythree
  sixtyfour
  sixtyfive
  sixtysix
  sixtyseven
  sixtyeight
  sixtynine
  seventy
  seventyone
  seventytwo
  seventythree
  seventyfour
  seventyfive
  seventysix
  seventyseven
  seventyeight
  seventynine
  eighty
  eightyone
  eightytwo
  eightythree
  eightyfour
  eightyfive
  eightysix
  eightyseven
  eightyeight
  eightynine
  ninety
  ninetyone
  ninetytwo
  ninetythree
  ninetyfour
  ninetyfive
  ninetysix
  ninetyseven
  ninetyeight
  ninetynine
  onehundred
  onehundredandone
  onehundredandtwo
  onehundredandthree
  onehundredandfour
  onehundredandfive
  onehundredandsix
  onehundredandseven
  onehundredandeight
  onehundredandnine
  onehundredandten
  onehundredandeleven
  onehundredandtwelve
  onehundredandthirteen
  onehundredandfourteen
  onehundredandfifteen
  onehundredandsixteen
  onehundredandseventeen
  onehundredandeighteen
  onehundredandnineteen
  onehundredandtwenty
  onehundredandtwentyone
  onehundredandtwentytwo
  onehundredandtwentythree
  onehundredandtwentyfour
  onehundredandtwentyfive
  onehundredandtwentysix
  onehundredandtwentyseven
  onehundredandtwentyeight
  onehundredandtwentynine
  onehundredandthirty
  onehundredandthirtyone
  onehundredandthirtytwo
  onehundredandthirtythree
  onehundredandthirtyfour
  onehundredandthirtyfive
  onehundredandthirtysix
  onehundredandthirtyseven
  onehundredandthirtyeight
  onehundredandthirtynine
  onehundredandfourty
  onehundredandfourtyone
  onehundredandfourtytwo
  onehundredandfourtythree
  onehundredandfourtyfour
  onehundredandfourtyfive
  onehundredandfourtysix
  onehundredandfourtyseven
  onehundredandfourtyeight
  onehundredandfourtynine
  onehundredandfifty
  onehundredandfiftyone
  onehundredandfiftytwo
  onehundredandfiftythree
  onehundredandfiftyfour
  onehundredandfiftyfive
  onehundredandfiftysix
  onehundredandfiftyseven
  onehundredandfiftyeight
  onehundredandfiftynine
  onehundredandsixty
  onehundredandsixtyone
  onehundredandsixtytwo
  onehundredandsixtythree
  onehundredandsixtyfour
  onehundredandsixtyfive
  onehundredandsixtysix
  onehundredandsixtyseven
  onehundredandsixtyeight
  onehundredandsixtynine
  onehundredandseventy
  onehundredandseventyone
  onehundredandseventytwo
  onehundredandseventythree
  onehundredandseventyfour
  onehundredandseventyfive
  onehundredandseventysix
  onehundredandseventyseven
  onehundredandseventyeight
  onehundredandseventynine
  onehundredandeighty
  onehundredandeightyone
  onehundredandeightytwo
  onehundredandeightythree
  onehundredandeightyfour
  onehundredandeightyfive
  onehundredandeightysix
  onehundredandeightyseven
  onehundredandeightyeight
  onehundredandeightynine
  onehundredandninety
  onehundredandninetyone
  onehundredandninetytwo
  onehundredandninetythree
  onehundredandninetyfour
  onehundredandninetyfive
  onehundredandninetysix
  onehundredandninetyseven
  onehundredandninetyeight
  onehundredandninetynine
  twohundred
  twohundredandone
  twohundredandtwo
  twohundredandthree
  twohundredandfour
  twohundredandfive
  twohundredandsix
  twohundredandseven
  twohundredandeight
  twohundredandnine
  twohundredandten
  twohundredandeleven
  twohundredandtwelve
  twohundredandthirteen
  twohundredandfourteen
  twohundredandfifteen
  twohundredandsixteen
  twohundredandseventeen
  twohundredandeighteen
  twohundredandnineteen
  twohundredandtwenty
  twohundredandtwentyone
  twohundredandtwentytwo
  twohundredandtwentythree
  twohundredandtwentyfour
  twohundredandtwentyfive
  twohundredandtwentysix
  twohundredandtwentyseven
  twohundredandtwentyeight
  twohundredandtwentynine
  twohundredandthirty
  twohundredandthirtyone
  twohundredandthirtytwo
  twohundredandthirtythree
  twohundredandthirtyfour
  twohundredandthirtyfive
  twohundredandthirtysix
  twohundredandthirtyseven
  twohundredandthirtyeight
  twohundredandthirtynine
  twohundredandfourty
  twohundredandfourtyone
  twohundredandfourtytwo
  twohundredandfourtythree
  twohundredandfourtyfour
  twohundredandfourtyfive
  twohundredandfourtysix
  twohundredandfourtyseven
  twohundredandfourtyeight
  twohundredandfourtynine
  twohundredandfifty
  twohundredandfiftyone
  twohundredandfiftytwo
  twohundredandfiftythree
  twohundredandfiftyfour
  twohundredandfiftyfive
  twohundredandfiftysix
  twohundredandfiftyseven
  twohundredandfiftyeight
  twohundredandfiftynine
  twohundredandsixty
  twohundredandsixtyone
  twohundredandsixtytwo
  twohundredandsixtythree
  twohundredandsixtyfour
  twohundredandsixtyfive
  twohundredandsixtysix
  twohundredandsixtyseven
  twohundredandsixtyeight
  twohundredandsixtynine
  twohundredandseventy
  twohundredandseventyone
  twohundredandseventytwo
  twohundredandseventythree
  twohundredandseventyfour
  twohundredandseventyfive
  twohundredandseventysix
  twohundredandseventyseven
  twohundredandseventyeight
  twohundredandseventynine
  twohundredandeighty
  twohundredandeightyone
  twohundredandeightytwo
  twohundredandeightythree
  twohundredandeightyfour
  twohundredandeightyfive
  twohundredandeightysix
  twohundredandeightyseven
  twohundredandeightyeight
  twohundredandeightynine
  twohundredandninety
  twohundredandninetyone
  twohundredandninetytwo
  twohundredandninetythree
  twohundredandninetyfour
  twohundredandninetyfive
  twohundredandninetysix
  twohundredandninetyseven
  twohundredandninetyeight
  twohundredandninetynine
  threehundred
  threehundredandone
  threehundredandtwo
  threehundredandthree
  threehundredandfour
  threehundredandfive
  threehundredandsix
  threehundredandseven
  threehundredandeight
  threehundredandnine
  threehundredandten
  threehundredandeleven
  threehundredandtwelve
  threehundredandthirteen
  threehundredandfourteen
  threehundredandfifteen
  threehundredandsixteen
  threehundredandseventeen
  threehundredandeighteen
  threehundredandnineteen
  threehundredandtwenty
  threehundredandtwentyone
  threehundredandtwentytwo
  threehundredandtwentythree
  threehundredandtwentyfour
  threehundredandtwentyfive
  threehundredandtwentysix
  threehundredandtwentyseven
  threehundredandtwentyeight
  threehundredandtwentynine
  threehundredandthirty
  threehundredandthirtyone
  threehundredandthirtytwo
  threehundredandthirtythree
  threehundredandthirtyfour
  threehundredandthirtyfive
  threehundredandthirtysix
  threehundredandthirtyseven
  threehundredandthirtyeight
  threehundredandthirtynine
  threehundredandfourty
  threehundredandfourtyone
  threehundredandfourtytwo
  threehundredandfourtythree
  threehundredandfourtyfour
  threehundredandfourtyfive
  threehundredandfourtysix
  threehundredandfourtyseven
  threehundredandfourtyeight
  threehundredandfourtynine
  threehundredandfifty
  threehundredandfiftyone
  threehundredandfiftytwo
  threehundredandfiftythree
  threehundredandfiftyfour
  threehundredandfiftyfive
  threehundredandfiftysix
  threehundredandfiftyseven
  threehundredandfiftyeight
  threehundredandfiftynine
  threehundredandsixty
  threehundredandsixtyone
  threehundredandsixtytwo
  threehundredandsixtythree
  threehundredandsixtyfour
  threehundredandsixtyfive
  threehundredandsixtysix
  threehundredandsixtyseven
  threehundredandsixtyeight
  threehundredandsixtynine
  threehundredandseventy
  threehundredandseventyone
  threehundredandseventytwo
  threehundredandseventythree
  threehundredandseventyfour
  threehundredandseventyfive
  threehundredandseventysix
  threehundredandseventyseven
  threehundredandseventyeight
  threehundredandseventynine
  threehundredandeighty
  threehundredandeightyone
  threehundredandeightytwo
  threehundredandeightythree
  threehundredandeightyfour
  threehundredandeightyfive
  threehundredandeightysix
  threehundredandeightyseven
  threehundredandeightyeight
  threehundredandeightynine
  threehundredandninety
  threehundredandninetyone
  threehundredandninetytwo
  threehundredandninetythree
  threehundredandninetyfour
  threehundredandninetyfive
  threehundredandninetysix
  threehundredandninetyseven
  threehundredandninetyeight
  threehundredandninetynine
  fourhundred
  fourhundredandone
  fourhundredandtwo
  fourhundredandthree
  fourhundredandfour
  fourhundredandfive
  fourhundredandsix
  fourhundredandseven
  fourhundredandeight
  fourhundredandnine
  fourhundredandten
  fourhundredandeleven
  fourhundredandtwelve
  fourhundredandthirteen
  fourhundredandfourteen
  fourhundredandfifteen
  fourhundredandsixteen
  fourhundredandseventeen
  fourhundredandeighteen
  fourhundredandnineteen
  fourhundredandtwenty
  fourhundredandtwentyone
  fourhundredandtwentytwo
  fourhundredandtwentythree
  fourhundredandtwentyfour
  fourhundredandtwentyfive
  fourhundredandtwentysix
  fourhundredandtwentyseven
  fourhundredandtwentyeight
  fourhundredandtwentynine
  fourhundredandthirty
  fourhundredandthirtyone
  fourhundredandthirtytwo
  fourhundredandthirtythree
  fourhundredandthirtyfour
  fourhundredandthirtyfive
  fourhundredandthirtysix
  fourhundredandthirtyseven
  fourhundredandthirtyeight
  fourhundredandthirtynine
  fourhundredandfourty
  fourhundredandfourtyone
  fourhundredandfourtytwo
  fourhundredandfourtythree
  fourhundredandfourtyfour
  fourhundredandfourtyfive
  fourhundredandfourtysix
  fourhundredandfourtyseven
  fourhundredandfourtyeight
  fourhundredandfourtynine
  fourhundredandfifty
  fourhundredandfiftyone
  fourhundredandfiftytwo
  fourhundredandfiftythree
  fourhundredandfiftyfour
  fourhundredandfiftyfive
  fourhundredandfiftysix
  fourhundredandfiftyseven
  fourhundredandfiftyeight
  fourhundredandfiftynine
  fourhundredandsixty
  fourhundredandsixtyone
  fourhundredandsixtytwo
  fourhundredandsixtythree
  fourhundredandsixtyfour
  fourhundredandsixtyfive
  fourhundredandsixtysix
  fourhundredandsixtyseven
  fourhundredandsixtyeight
  fourhundredandsixtynine
  fourhundredandseventy
  fourhundredandseventyone
  fourhundredandseventytwo
  fourhundredandseventythree
  fourhundredandseventyfour
  fourhundredandseventyfive
  fourhundredandseventysix
  fourhundredandseventyseven
  fourhundredandseventyeight
  fourhundredandseventynine
  fourhundredandeighty
  fourhundredandeightyone
  fourhundredandeightytwo
  fourhundredandeightythree
  fourhundredandeightyfour
  fourhundredandeightyfive
  fourhundredandeightysix
  fourhundredandeightyseven
  fourhundredandeightyeight
  fourhundredandeightynine
  fourhundredandninety
  fourhundredandninetyone
  fourhundredandninetytwo
  fourhundredandninetythree
  fourhundredandninetyfour
  fourhundredandninetyfive
  fourhundredandninetysix
  fourhundredandninetyseven
  fourhundredandninetyeight
  fourhundredandninetynine
  fivehundred
  fivehundredandone
  fivehundredandtwo
  fivehundredandthree
  fivehundredandfour
  fivehundredandfive
  fivehundredandsix
  fivehundredandseven
  fivehundredandeight
  fivehundredandnine
  fivehundredandten
  fivehundredandeleven
  fivehundredandtwelve
  fivehundredandthirteen
  fivehundredandfourteen
  fivehundredandfifteen
  fivehundredandsixteen
  fivehundredandseventeen
  fivehundredandeighteen
  fivehundredandnineteen
  fivehundredandtwenty
  fivehundredandtwentyone
  fivehundredandtwentytwo
  fivehundredandtwentythree
  fivehundredandtwentyfour
  fivehundredandtwentyfive
  fivehundredandtwentysix
  fivehundredandtwentyseven
  fivehundredandtwentyeight
  fivehundredandtwentynine
  fivehundredandthirty
  fivehundredandthirtyone
  fivehundredandthirtytwo
  fivehundredandthirtythree
  fivehundredandthirtyfour
  fivehundredandthirtyfive
  fivehundredandthirtysix
  fivehundredandthirtyseven
  fivehundredandthirtyeight
  fivehundredandthirtynine
  fivehundredandfourty
  fivehundredandfourtyone
  fivehundredandfourtytwo
  fivehundredandfourtythree
  fivehundredandfourtyfour
  fivehundredandfourtyfive
  fivehundredandfourtysix
  fivehundredandfourtyseven
  fivehundredandfourtyeight
  fivehundredandfourtynine
  fivehundredandfifty
  fivehundredandfiftyone
  fivehundredandfiftytwo
  fivehundredandfiftythree
  fivehundredandfiftyfour
  fivehundredandfiftyfive
  fivehundredandfiftysix
  fivehundredandfiftyseven
  fivehundredandfiftyeight
  fivehundredandfiftynine
  fivehundredandsixty
  fivehundredandsixtyone
  fivehundredandsixtytwo
  fivehundredandsixtythree
  fivehundredandsixtyfour
  fivehundredandsixtyfive
  fivehundredandsixtysix
  fivehundredandsixtyseven
  fivehundredandsixtyeight
  fivehundredandsixtynine
  fivehundredandseventy
  fivehundredandseventyone
  fivehundredandseventytwo
  fivehundredandseventythree
  fivehundredandseventyfour
  fivehundredandseventyfive
  fivehundredandseventysix
  fivehundredandseventyseven
  fivehundredandseventyeight
  fivehundredandseventynine
  fivehundredandeighty
  fivehundredandeightyone
  fivehundredandeightytwo
  fivehundredandeightythree
  fivehundredandeightyfour
  fivehundredandeightyfive
  fivehundredandeightysix
  fivehundredandeightyseven
  fivehundredandeightyeight
  fivehundredandeightynine
  fivehundredandninety
  fivehundredandninetyone
  fivehundredandninetytwo
  fivehundredandninetythree
  fivehundredandninetyfour
  fivehundredandninetyfive
  fivehundredandninetysix
  fivehundredandninetyseven
  fivehundredandninetyeight
  fivehundredandninetynine
  sixhundred
  .....
  ninehundredandninetyseven
  ninehundredandninetyeight
  ninehundredandninetynine
  onethousand
  </code></pre>
  
  <p>It looks right to me...</p>
  
  <p>If I test it with their examples using <code>for x in range(1, 6)</code> comes out with 19</p>
  
  <p>If I do <code>len(convert(115))</code> it says <code>20</code> characters, but if I do <code>len(convert(342))</code> it comes out with <code>24</code> and not <code>23</code>... so what did I do wrong?</p>