我是python中的初学程序员,我需要一些代码的帮助。附上我一直在研究的代码。目的是获得像305.67这样的美元金额,并将数字转换为“三百五十美元和六十七美分”之类的文本短语。到目前为止,我已经获得了大部分代码将其分解为文本,但我仍然遇到数字11-19的问题,这是一个特例。我需要帮助弄清楚程序如何正确地决定在哪里应用11-19以及何时在不需要时删除“ZERO”字符串。如果你运行该程序,你会看到我的意思。主函数中的下面的循环将根据函数给定您希望的数量来运行循环。此外,还有一个名为“getDollarFormatText”的函数,它将采用货币的数字版本,如305.67或45.13,并为您提供文本格式。我遇到的问题是如何让程序忽略小数,然后将所有内容适当地转换为小数的左右两边。这是一个负载问题,我将彻底欣赏这一点。基本上这个问题很容易解决,但我不知道如何解决它。代码以一个处理两位数字的函数开始(我已经处理了仅用一位数字分隔的函数)。
def getWordForTwoDigits(amount):
#This value uses integer division to get the number of tens within a number.
tensAmount = int(amount) / 10
#This variable uses the first function above to find the equivalent single number within the number given using modulo. (Zero through 9)
singlesWord = getWordForDigit(int(amount)%10)
#For this decision structure, the structure is set to figuring out the number of tens within a number and appropriately naming it after its correct name (ten, twenty, etc.)
if tensAmount == 1:
wordTen = "TEN"
else:
if tensAmount == 2:
wordTen = "TWENTY"
else:
if tensAmount == 3:
wordTen = "THIRTY"
else:
if tensAmount == 4:
wordTen = "FORTY"
else:
if tensAmount == 5:
wordTen = "FIFTY"
else:
if tensAmount == 6:
wordTen = "SIXTY"
else:
if tensAmount == 7:
wordTen = "SEVENTY"
else:
if tensAmount == 8:
wordTen = "EIGHTY"
else:
if tensAmount == 9:
wordTen = "NINETY"
return "%s-%s"%(wordTen, singlesWord)
########################################
def getWordForThreeDigits(dolamount):
hundredAmount = int(dolamount) / 100
twoDigits = getWordForTwoDigits(int(dolamount) % 100)
if hundredAmount == 0:
return twoDigits
else:
if hundredAmount == 1:
wordHun = "ONE HUNDRED"
else:
if hundredAmount == 2:
wordHun = "TWO HUNDRED"
else:
if hundredAmount == 3:
wordHun = "THREE HUNDRED"
else:
if hundredAmount == 4:
wordHun = "FOUR HUNDRED"
else:
if hundredAmount == 5:
wordHun = "FIVE HUNDRED"
else:
if hundredAmount == 6:
wordHun = "SIX HUNDRED"
else:
if hundredAmount == 7:
wordHun = "SEVEN HUNDRED"
else:
if hundredAmount == 8:
wordHun = "EIGHT HUNDRED"
else:
if hundredAmount == 9:
wordHun = "NINE HUNDRED"
return "%s %s"%(wordHun, twoDigits)
####################################
def getDollarFormatText(dollarAndCents):
#how would you separate 190.67 (example) to 190 and 67 and and give the text form for eacn
答案 0 :(得分:3)
为了您的练习,我们调整了一个现有的优秀解决方案ref,用于将数字转换为单词,如下所示:
def numToWords(num,join=True):
'''words = {} convert an integer number into words'''
units = ['','one','two','three','four','five','six','seven','eight','nine']
teens = ['','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \
'seventeen','eighteen','nineteen']
tens = ['','ten','twenty','thirty','forty','fifty','sixty','seventy', \
'eighty','ninety']
thousands = ['','thousand','million','billion','trillion','quadrillion', \
'quintillion','sextillion','septillion','octillion', \
'nonillion','decillion','undecillion','duodecillion', \
'tredecillion','quattuordecillion','sexdecillion', \
'septendecillion','octodecillion','novemdecillion', \
'vigintillion']
words = []
if num==0: words.append('zero')
else:
numStr = '%d'%num
numStrLen = len(numStr)
groups = (numStrLen+2)/3
numStr = numStr.zfill(groups*3)
for i in range(0,groups*3,3):
h,t,u = int(numStr[i]),int(numStr[i+1]),int(numStr[i+2])
g = groups-(i/3+1)
if h>=1:
words.append(units[h])
words.append('hundred')
if t>1:
words.append(tens[t])
if u>=1: words.append(units[u])
elif t==1:
if u>=1: words.append(teens[u])
else: words.append(tens[t])
else:
if u>=1: words.append(units[u])
if (g>=1) and ((h+t+u)>0): words.append(thousands[g]+',')
if join: return ' '.join(words)
return words
#example usages:
print numToWords(0)
print numToWords(11)
print numToWords(110)
print numToWords(1001000025)
print numToWords(123456789012)
结果:
zero
eleven
one hundred ten
one billion, one million, twenty five
one hundred twenty three billion, four hundred fifty six million, seven hundred
eighty nine thousand, twelve
请注意,它适用于整数。然而,将浮点数除以两个整数部分是微不足道的。
答案 1 :(得分:2)
好的......这里有很多问题。让我们从一些基本的语法开始。
首先,您需要回过头来看看Python中的格式化工作原理。它适用于四个空格,如果您使用的是正确的处理器,则相当于“标签”。在定义函数时,该函数中包含的所有内容应至少为4个空格。在你的函数“getWordForTwoDigits()”中你有这个:
def getWordForTwoDigits(amount):
...
return ...
返回应该是四个空格(你为这两个函数做了这个,BTW)。
其次,你的else-if结构是棘手的,不需要的。而不是你现在拥有的,只需这样做:
if TensAmount == 1:
do something
elif TensAmount == 2:
do something different
然后只需将更多'elif'添加到9。
另外,你说
singlesWord = getWordForDigit(int(amount)%10)
但你永远不会定义那个功能。
关于如何分离196和97的实际问题,尝试这样的事情:
def splitter(num):
sep=str(num).rsplit('.', 1)
return sep
'rsplit'基本上根据第一个参数中的char将字符串拆分为两部分。它返回一个列表[“partone”,“parttwo”],因此您需要提取列表的两个部分并将它们转换回主代码中的int。
希望有所帮助!
答案 2 :(得分:1)
使用elif
代替
else:
if
快速修复。 对于,部分处理11-19,之前
tensAmount = int(amount) / 10
使用
if 10<amount<20:
#your statements to handle 11-19
else:
#your regular statements to divide by 10 again
但我强烈建议你使用字典,让你这样开始。
singleDigitDict={1="one", 2="Two"}#fill up
wordSingleDigit=singleDigitDict[singleDigitAmount]