python - Word to Number project - Stack Overflow

word=(input("What is the numberDON'T TYPE IN AND,-,_,or ,. ",)).lower().split()wordnu

word=(input("What is the number  DON'T TYPE IN AND,-,_,or ,. ",)).lower().split()
wordnum={
"zero":0,
"one":1,
"two":2,
"three":3,
"four":4,
"five":5,
"six":6,
"seven":7,
"eight":8,
"nine":9,
"ten":10,
"eleven":11,
"twelve":12,
"thirteen":13,
"fourteen":14,
"fifteen":15,
"sixteen":16,
"seventeen":17,
"eighteen":18,
"nineteen":19,
"twenty":20,
"thirty":30,
"forty":40,
"fifty":50,
"sixty":60,
"seventy":70,
"eighty":80,
"ninety":90,
"hundred":100,
"thousand":1000,
"million":1000000
}
current=0

final=0
mult=1

for number in word:
    if number in wordnum:
        
        value=wordnum[number]
    
        if  value< 100:
            current += value


        elif value >= 100:  # This is a multiplier
            current*= value
            mult = value

final += current

format=f"{final:,}"
print(format)

So I'm using this to make it so a user can input a word and it will convert it to a number but when i input three hundred fifty thousand four hundred twelve it outputs 35,000,412 and i don't understand what's wrong. does someone know the answer?

word=(input("What is the number  DON'T TYPE IN AND,-,_,or ,. ",)).lower().split()
wordnum={
"zero":0,
"one":1,
"two":2,
"three":3,
"four":4,
"five":5,
"six":6,
"seven":7,
"eight":8,
"nine":9,
"ten":10,
"eleven":11,
"twelve":12,
"thirteen":13,
"fourteen":14,
"fifteen":15,
"sixteen":16,
"seventeen":17,
"eighteen":18,
"nineteen":19,
"twenty":20,
"thirty":30,
"forty":40,
"fifty":50,
"sixty":60,
"seventy":70,
"eighty":80,
"ninety":90,
"hundred":100,
"thousand":1000,
"million":1000000
}
current=0

final=0
mult=1

for number in word:
    if number in wordnum:
        
        value=wordnum[number]
    
        if  value< 100:
            current += value


        elif value >= 100:  # This is a multiplier
            current*= value
            mult = value

final += current

format=f"{final:,}"
print(format)

So I'm using this to make it so a user can input a word and it will convert it to a number but when i input three hundred fifty thousand four hundred twelve it outputs 35,000,412 and i don't understand what's wrong. does someone know the answer?

Share Improve this question edited Mar 6 at 16:45 OldBoy 9261 gold badge3 silver badges14 bronze badges asked Mar 6 at 16:24 Aleksander GodfreyAleksander Godfrey 11 5
  • 1 Add some print statements in your code so you can see what values are being calculated at each step. That should help you to see why it is not working correctly. – OldBoy Commented Mar 6 at 16:36
  • Also try seeing if a number without any zeros works correctly (ex. try three hundred fifty one thousand four hundred twelve) – Starship Remembers Shadow Commented Mar 6 at 16:54
  • The code correctly calculates 350000 for "three hundred fifty thousand", but then it adds 4 because of the word "four" and multiplies by 100 because of the word "hundred". – John Gordon Commented Mar 6 at 16:56
  • Do you realize that the variables final and mult have no effect? Perhaps you intended to use them for some extra processing, but fot to actually add it? – John Gordon Commented Mar 6 at 16:58
  • ericlippert/2014/03/05/how-to-debug-small-programs – John Gordon Commented Mar 6 at 17:10
Add a comment  | 

1 Answer 1

Reset to default 1

You need to add to the final counter and reset the current counter each time your value is a thousand, million, etc. So, you can add another if statement to you loop like (and import the math library):

import math
...

current=0
final=0

for number in word:
    if number in wordnum:
        
        value=wordnum[number]
    
        if  value < 100:
            current += value
        elif value >= 100:  # This is a multiplier
            current *= value
            
        if math.log10(value) % 3 == 0:
            # if value is 1000, 1000000, etc add on to final and reset current
            final += current
            current = 0  # reset current

# add on further values
final += current

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744962872a4603495.html

相关推荐

  • python - Word to Number project - Stack Overflow

    word=(input("What is the numberDON'T TYPE IN AND,-,_,or ,. ",)).lower().split()wordnu

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信