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 Answer
Reset to default 1You 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
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:36final
andmult
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