How to convert numbers to words python 3



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have an issue when trying to convert numbers to words: it crashes when I enter 80, or if i enter 100 it produces "one hundred two hundred". Can someone help me fix my code.



number = input("Enter a value: ")
ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ",
"eight ", "nine "]
teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ", "nineteen "]
decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy
", "eighty ", "ninety "]
hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four
hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",
"nine hundred"]

word = ""
change = len(number)

while change > 0:
if number == "0":
word ="zero"
break
elif change > 1 and number[change - 2] == "1":
for i in range(0,10):
if number[change - 1] == str(i):
word = teens[i] + word
else:
for i in range(0,10):
if number[change - 1] == str(i):
word = ones[i] + word
if change > 1:
for i in range(0,10):
if number[change - 2] == str(i):
word = decades[i] + word
if change > 2:
for i in range(0,10):
if number[change - 3] == str(i):
word = hundreds[i] + word
change = change - 3
print(word)









share|improve this question






























    1















    I have an issue when trying to convert numbers to words: it crashes when I enter 80, or if i enter 100 it produces "one hundred two hundred". Can someone help me fix my code.



    number = input("Enter a value: ")
    ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ",
    "eight ", "nine "]
    teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ",
    "sixteen ", "seventeen ", "eighteen ", "nineteen "]
    decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy
    ", "eighty ", "ninety "]
    hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four
    hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",
    "nine hundred"]

    word = ""
    change = len(number)

    while change > 0:
    if number == "0":
    word ="zero"
    break
    elif change > 1 and number[change - 2] == "1":
    for i in range(0,10):
    if number[change - 1] == str(i):
    word = teens[i] + word
    else:
    for i in range(0,10):
    if number[change - 1] == str(i):
    word = ones[i] + word
    if change > 1:
    for i in range(0,10):
    if number[change - 2] == str(i):
    word = decades[i] + word
    if change > 2:
    for i in range(0,10):
    if number[change - 3] == str(i):
    word = hundreds[i] + word
    change = change - 3
    print(word)









    share|improve this question


























      1












      1








      1








      I have an issue when trying to convert numbers to words: it crashes when I enter 80, or if i enter 100 it produces "one hundred two hundred". Can someone help me fix my code.



      number = input("Enter a value: ")
      ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ",
      "eight ", "nine "]
      teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ",
      "sixteen ", "seventeen ", "eighteen ", "nineteen "]
      decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy
      ", "eighty ", "ninety "]
      hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four
      hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",
      "nine hundred"]

      word = ""
      change = len(number)

      while change > 0:
      if number == "0":
      word ="zero"
      break
      elif change > 1 and number[change - 2] == "1":
      for i in range(0,10):
      if number[change - 1] == str(i):
      word = teens[i] + word
      else:
      for i in range(0,10):
      if number[change - 1] == str(i):
      word = ones[i] + word
      if change > 1:
      for i in range(0,10):
      if number[change - 2] == str(i):
      word = decades[i] + word
      if change > 2:
      for i in range(0,10):
      if number[change - 3] == str(i):
      word = hundreds[i] + word
      change = change - 3
      print(word)









      share|improve this question
















      I have an issue when trying to convert numbers to words: it crashes when I enter 80, or if i enter 100 it produces "one hundred two hundred". Can someone help me fix my code.



      number = input("Enter a value: ")
      ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ",
      "eight ", "nine "]
      teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ",
      "sixteen ", "seventeen ", "eighteen ", "nineteen "]
      decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy
      ", "eighty ", "ninety "]
      hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four
      hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",
      "nine hundred"]

      word = ""
      change = len(number)

      while change > 0:
      if number == "0":
      word ="zero"
      break
      elif change > 1 and number[change - 2] == "1":
      for i in range(0,10):
      if number[change - 1] == str(i):
      word = teens[i] + word
      else:
      for i in range(0,10):
      if number[change - 1] == str(i):
      word = ones[i] + word
      if change > 1:
      for i in range(0,10):
      if number[change - 2] == str(i):
      word = decades[i] + word
      if change > 2:
      for i in range(0,10):
      if number[change - 3] == str(i):
      word = hundreds[i] + word
      change = change - 3
      print(word)






      python python-3.x for-loop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 10:38







      WhiteFlower

















      asked Nov 15 '18 at 10:18









      WhiteFlowerWhiteFlower

      124




      124






















          5 Answers
          5






          active

          oldest

          votes


















          0














          This should work: word2number



          And an example from their docs:
          print(w2n.word_to_num('one hundred thirty-five'))






          share|improve this answer























          • I don't think OP wants to use a library.

            – Vineeth Sai
            Nov 15 '18 at 10:24











          • @Yasen I do not want to use a library for this applications, thanks though

            – WhiteFlower
            Nov 15 '18 at 10:31


















          0














          Just this if statements will work (note that's all you need to make it work):



          if len(number)==1:
          print(ones[int(number)])
          elif len(number)==2 and number[0]=='1':
          print(teens[int(number[-1])])
          elif len(number)==2 and number[0]!='1':
          print(decades[int(number[0])-1])
          elif len(number)==3:
          print(hundreds[int(number[0])])


          Note that have to add commas in decades and hundreds, so copy the below, then replace with the clipboard:



          decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
          hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",





          share|improve this answer


















          • 2





            Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

            – WhiteFlower
            Nov 15 '18 at 10:37



















          0














          You have missed a comma between "seventy" and "eighty" in decades. You have to change decades to the following:



          decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]


          The same mistake is there in hunderds list as well. Use this new list:



          hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]


          Nevertheless, your implementation has lot of mistakes.






          share|improve this answer

























          • Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

            – WhiteFlower
            Nov 15 '18 at 10:33






          • 2





            Note: hundreds list also has the same problem...

            – U9-Forward
            Nov 15 '18 at 10:33











          • @WhiteFlower Use: word = decades[i-1] + word

            – Ernest S Kirubakaran
            Nov 15 '18 at 10:36











          • @U9-Forward updated my answer

            – Ernest S Kirubakaran
            Nov 15 '18 at 10:38











          • @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

            – WhiteFlower
            Nov 15 '18 at 10:40



















          0














          I think that for making your example working you just need to make sure that each change digit is accessing the correct value in your lists. That's not the case for your decades lists, where you missed a blank string for the teens, that you're correctly treating as a separate case.



          So to make your code work just change:



          decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]


          to this:



          decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]





          share|improve this answer






























            0














            For this implementation, you should always have 10 elements in the lists. For example, in decades you should have some string representing the values between 10-20 even if its not used because the indexing of the array would be wrong otherwise.



            Still this code has a lot of mistakes.
            Try this.



            number = input("Enter a value: ")
            ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
            teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
            decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
            hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

            word = ""
            change = len(number)

            while change > 0:
            if number == "0":
            word ="zero"
            break
            elif change > 1 and number[change - 2] == "1":
            for i in range(0,10):
            if number[change - 1] == str(i):
            word = teens[i] + word
            if change == 2:
            break
            else:
            for i in range(0,10):
            if number[change - 1] == str(i):
            word = ones[i] + word
            if change > 1 and number[change-2]!="1":
            for i in range(0,10):
            if number[change - 2] == str(i):
            word = decades[i] + word
            if change > 2:
            for i in range(0,10):
            if number[change - 3] == str(i):
            word = hundreds[i] + word
            change = change - 3
            print(word)





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317174%2fhow-to-convert-numbers-to-words-python-3%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              This should work: word2number



              And an example from their docs:
              print(w2n.word_to_num('one hundred thirty-five'))






              share|improve this answer























              • I don't think OP wants to use a library.

                – Vineeth Sai
                Nov 15 '18 at 10:24











              • @Yasen I do not want to use a library for this applications, thanks though

                – WhiteFlower
                Nov 15 '18 at 10:31















              0














              This should work: word2number



              And an example from their docs:
              print(w2n.word_to_num('one hundred thirty-five'))






              share|improve this answer























              • I don't think OP wants to use a library.

                – Vineeth Sai
                Nov 15 '18 at 10:24











              • @Yasen I do not want to use a library for this applications, thanks though

                – WhiteFlower
                Nov 15 '18 at 10:31













              0












              0








              0







              This should work: word2number



              And an example from their docs:
              print(w2n.word_to_num('one hundred thirty-five'))






              share|improve this answer













              This should work: word2number



              And an example from their docs:
              print(w2n.word_to_num('one hundred thirty-five'))







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 10:23









              YasenYasen

              1,559816




              1,559816












              • I don't think OP wants to use a library.

                – Vineeth Sai
                Nov 15 '18 at 10:24











              • @Yasen I do not want to use a library for this applications, thanks though

                – WhiteFlower
                Nov 15 '18 at 10:31

















              • I don't think OP wants to use a library.

                – Vineeth Sai
                Nov 15 '18 at 10:24











              • @Yasen I do not want to use a library for this applications, thanks though

                – WhiteFlower
                Nov 15 '18 at 10:31
















              I don't think OP wants to use a library.

              – Vineeth Sai
              Nov 15 '18 at 10:24





              I don't think OP wants to use a library.

              – Vineeth Sai
              Nov 15 '18 at 10:24













              @Yasen I do not want to use a library for this applications, thanks though

              – WhiteFlower
              Nov 15 '18 at 10:31





              @Yasen I do not want to use a library for this applications, thanks though

              – WhiteFlower
              Nov 15 '18 at 10:31













              0














              Just this if statements will work (note that's all you need to make it work):



              if len(number)==1:
              print(ones[int(number)])
              elif len(number)==2 and number[0]=='1':
              print(teens[int(number[-1])])
              elif len(number)==2 and number[0]!='1':
              print(decades[int(number[0])-1])
              elif len(number)==3:
              print(hundreds[int(number[0])])


              Note that have to add commas in decades and hundreds, so copy the below, then replace with the clipboard:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",





              share|improve this answer


















              • 2





                Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

                – WhiteFlower
                Nov 15 '18 at 10:37
















              0














              Just this if statements will work (note that's all you need to make it work):



              if len(number)==1:
              print(ones[int(number)])
              elif len(number)==2 and number[0]=='1':
              print(teens[int(number[-1])])
              elif len(number)==2 and number[0]!='1':
              print(decades[int(number[0])-1])
              elif len(number)==3:
              print(hundreds[int(number[0])])


              Note that have to add commas in decades and hundreds, so copy the below, then replace with the clipboard:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",





              share|improve this answer


















              • 2





                Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

                – WhiteFlower
                Nov 15 '18 at 10:37














              0












              0








              0







              Just this if statements will work (note that's all you need to make it work):



              if len(number)==1:
              print(ones[int(number)])
              elif len(number)==2 and number[0]=='1':
              print(teens[int(number[-1])])
              elif len(number)==2 and number[0]!='1':
              print(decades[int(number[0])-1])
              elif len(number)==3:
              print(hundreds[int(number[0])])


              Note that have to add commas in decades and hundreds, so copy the below, then replace with the clipboard:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",





              share|improve this answer













              Just this if statements will work (note that's all you need to make it work):



              if len(number)==1:
              print(ones[int(number)])
              elif len(number)==2 and number[0]=='1':
              print(teens[int(number[-1])])
              elif len(number)==2 and number[0]!='1':
              print(decades[int(number[0])-1])
              elif len(number)==3:
              print(hundreds[int(number[0])])


              Note that have to add commas in decades and hundreds, so copy the below, then replace with the clipboard:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]
              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred",






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 10:33









              U9-ForwardU9-Forward

              18.1k51744




              18.1k51744







              • 2





                Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

                – WhiteFlower
                Nov 15 '18 at 10:37













              • 2





                Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

                – WhiteFlower
                Nov 15 '18 at 10:37








              2




              2





              Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

              – WhiteFlower
              Nov 15 '18 at 10:37






              Thanks for the help. I have tried this it works only for certain cases like 80, 10, 5 etc but if i try 153 or 86 etc it just returns "one hundred" or "eighty".

              – WhiteFlower
              Nov 15 '18 at 10:37












              0














              You have missed a comma between "seventy" and "eighty" in decades. You have to change decades to the following:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]


              The same mistake is there in hunderds list as well. Use this new list:



              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]


              Nevertheless, your implementation has lot of mistakes.






              share|improve this answer

























              • Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

                – WhiteFlower
                Nov 15 '18 at 10:33






              • 2





                Note: hundreds list also has the same problem...

                – U9-Forward
                Nov 15 '18 at 10:33











              • @WhiteFlower Use: word = decades[i-1] + word

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:36











              • @U9-Forward updated my answer

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:38











              • @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

                – WhiteFlower
                Nov 15 '18 at 10:40
















              0














              You have missed a comma between "seventy" and "eighty" in decades. You have to change decades to the following:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]


              The same mistake is there in hunderds list as well. Use this new list:



              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]


              Nevertheless, your implementation has lot of mistakes.






              share|improve this answer

























              • Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

                – WhiteFlower
                Nov 15 '18 at 10:33






              • 2





                Note: hundreds list also has the same problem...

                – U9-Forward
                Nov 15 '18 at 10:33











              • @WhiteFlower Use: word = decades[i-1] + word

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:36











              • @U9-Forward updated my answer

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:38











              • @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

                – WhiteFlower
                Nov 15 '18 at 10:40














              0












              0








              0







              You have missed a comma between "seventy" and "eighty" in decades. You have to change decades to the following:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]


              The same mistake is there in hunderds list as well. Use this new list:



              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]


              Nevertheless, your implementation has lot of mistakes.






              share|improve this answer















              You have missed a comma between "seventy" and "eighty" in decades. You have to change decades to the following:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety"]


              The same mistake is there in hunderds list as well. Use this new list:



              hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]


              Nevertheless, your implementation has lot of mistakes.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 10:38

























              answered Nov 15 '18 at 10:31









              Ernest S KirubakaranErnest S Kirubakaran

              911510




              911510












              • Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

                – WhiteFlower
                Nov 15 '18 at 10:33






              • 2





                Note: hundreds list also has the same problem...

                – U9-Forward
                Nov 15 '18 at 10:33











              • @WhiteFlower Use: word = decades[i-1] + word

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:36











              • @U9-Forward updated my answer

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:38











              • @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

                – WhiteFlower
                Nov 15 '18 at 10:40


















              • Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

                – WhiteFlower
                Nov 15 '18 at 10:33






              • 2





                Note: hundreds list also has the same problem...

                – U9-Forward
                Nov 15 '18 at 10:33











              • @WhiteFlower Use: word = decades[i-1] + word

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:36











              • @U9-Forward updated my answer

                – Ernest S Kirubakaran
                Nov 15 '18 at 10:38











              • @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

                – WhiteFlower
                Nov 15 '18 at 10:40

















              Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

              – WhiteFlower
              Nov 15 '18 at 10:33





              Thanks for that. However, when I enter 80, it is returning 90 and I am unsure as to why if you could help.

              – WhiteFlower
              Nov 15 '18 at 10:33




              2




              2





              Note: hundreds list also has the same problem...

              – U9-Forward
              Nov 15 '18 at 10:33





              Note: hundreds list also has the same problem...

              – U9-Forward
              Nov 15 '18 at 10:33













              @WhiteFlower Use: word = decades[i-1] + word

              – Ernest S Kirubakaran
              Nov 15 '18 at 10:36





              @WhiteFlower Use: word = decades[i-1] + word

              – Ernest S Kirubakaran
              Nov 15 '18 at 10:36













              @U9-Forward updated my answer

              – Ernest S Kirubakaran
              Nov 15 '18 at 10:38





              @U9-Forward updated my answer

              – Ernest S Kirubakaran
              Nov 15 '18 at 10:38













              @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

              – WhiteFlower
              Nov 15 '18 at 10:40






              @ErnestSKirubakaran Thanks, I have updated the comma mistakes. I have tried implementing this, however when i enter 95 it returns "five".

              – WhiteFlower
              Nov 15 '18 at 10:40












              0














              I think that for making your example working you just need to make sure that each change digit is accessing the correct value in your lists. That's not the case for your decades lists, where you missed a blank string for the teens, that you're correctly treating as a separate case.



              So to make your code work just change:



              decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]


              to this:



              decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]





              share|improve this answer



























                0














                I think that for making your example working you just need to make sure that each change digit is accessing the correct value in your lists. That's not the case for your decades lists, where you missed a blank string for the teens, that you're correctly treating as a separate case.



                So to make your code work just change:



                decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]


                to this:



                decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]





                share|improve this answer

























                  0












                  0








                  0







                  I think that for making your example working you just need to make sure that each change digit is accessing the correct value in your lists. That's not the case for your decades lists, where you missed a blank string for the teens, that you're correctly treating as a separate case.



                  So to make your code work just change:



                  decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]


                  to this:



                  decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]





                  share|improve this answer













                  I think that for making your example working you just need to make sure that each change digit is accessing the correct value in your lists. That's not the case for your decades lists, where you missed a blank string for the teens, that you're correctly treating as a separate case.



                  So to make your code work just change:



                  decades = ["", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]


                  to this:



                  decades = ["", "", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy", "eighty ", "ninety "]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 11:01









                  toti08toti08

                  1,80941623




                  1,80941623





















                      0














                      For this implementation, you should always have 10 elements in the lists. For example, in decades you should have some string representing the values between 10-20 even if its not used because the indexing of the array would be wrong otherwise.



                      Still this code has a lot of mistakes.
                      Try this.



                      number = input("Enter a value: ")
                      ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
                      teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
                      decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
                      hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

                      word = ""
                      change = len(number)

                      while change > 0:
                      if number == "0":
                      word ="zero"
                      break
                      elif change > 1 and number[change - 2] == "1":
                      for i in range(0,10):
                      if number[change - 1] == str(i):
                      word = teens[i] + word
                      if change == 2:
                      break
                      else:
                      for i in range(0,10):
                      if number[change - 1] == str(i):
                      word = ones[i] + word
                      if change > 1 and number[change-2]!="1":
                      for i in range(0,10):
                      if number[change - 2] == str(i):
                      word = decades[i] + word
                      if change > 2:
                      for i in range(0,10):
                      if number[change - 3] == str(i):
                      word = hundreds[i] + word
                      change = change - 3
                      print(word)





                      share|improve this answer



























                        0














                        For this implementation, you should always have 10 elements in the lists. For example, in decades you should have some string representing the values between 10-20 even if its not used because the indexing of the array would be wrong otherwise.



                        Still this code has a lot of mistakes.
                        Try this.



                        number = input("Enter a value: ")
                        ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
                        teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
                        decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
                        hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

                        word = ""
                        change = len(number)

                        while change > 0:
                        if number == "0":
                        word ="zero"
                        break
                        elif change > 1 and number[change - 2] == "1":
                        for i in range(0,10):
                        if number[change - 1] == str(i):
                        word = teens[i] + word
                        if change == 2:
                        break
                        else:
                        for i in range(0,10):
                        if number[change - 1] == str(i):
                        word = ones[i] + word
                        if change > 1 and number[change-2]!="1":
                        for i in range(0,10):
                        if number[change - 2] == str(i):
                        word = decades[i] + word
                        if change > 2:
                        for i in range(0,10):
                        if number[change - 3] == str(i):
                        word = hundreds[i] + word
                        change = change - 3
                        print(word)





                        share|improve this answer

























                          0












                          0








                          0







                          For this implementation, you should always have 10 elements in the lists. For example, in decades you should have some string representing the values between 10-20 even if its not used because the indexing of the array would be wrong otherwise.



                          Still this code has a lot of mistakes.
                          Try this.



                          number = input("Enter a value: ")
                          ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
                          teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
                          decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
                          hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

                          word = ""
                          change = len(number)

                          while change > 0:
                          if number == "0":
                          word ="zero"
                          break
                          elif change > 1 and number[change - 2] == "1":
                          for i in range(0,10):
                          if number[change - 1] == str(i):
                          word = teens[i] + word
                          if change == 2:
                          break
                          else:
                          for i in range(0,10):
                          if number[change - 1] == str(i):
                          word = ones[i] + word
                          if change > 1 and number[change-2]!="1":
                          for i in range(0,10):
                          if number[change - 2] == str(i):
                          word = decades[i] + word
                          if change > 2:
                          for i in range(0,10):
                          if number[change - 3] == str(i):
                          word = hundreds[i] + word
                          change = change - 3
                          print(word)





                          share|improve this answer













                          For this implementation, you should always have 10 elements in the lists. For example, in decades you should have some string representing the values between 10-20 even if its not used because the indexing of the array would be wrong otherwise.



                          Still this code has a lot of mistakes.
                          Try this.



                          number = input("Enter a value: ")
                          ones = ["", "one ", "two ","three ", "four", "five ", "six ", "seven ", "eight ", "nine "]
                          teens = ["ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "]
                          decades = ["", "onety", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "]
                          hundreds = ["", "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred", "six hundred ", "seven hundred", "eight hundred", "nine hundred"]

                          word = ""
                          change = len(number)

                          while change > 0:
                          if number == "0":
                          word ="zero"
                          break
                          elif change > 1 and number[change - 2] == "1":
                          for i in range(0,10):
                          if number[change - 1] == str(i):
                          word = teens[i] + word
                          if change == 2:
                          break
                          else:
                          for i in range(0,10):
                          if number[change - 1] == str(i):
                          word = ones[i] + word
                          if change > 1 and number[change-2]!="1":
                          for i in range(0,10):
                          if number[change - 2] == str(i):
                          word = decades[i] + word
                          if change > 2:
                          for i in range(0,10):
                          if number[change - 3] == str(i):
                          word = hundreds[i] + word
                          change = change - 3
                          print(word)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 11:03









                          RakihthaRRRakihthaRR

                          304313




                          304313



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317174%2fhow-to-convert-numbers-to-words-python-3%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo