【问题标题】:Beginner Python: If-else statements, formatting errors初学者 Python:If-else 语句,格式错误
【发布时间】:2013-10-12 04:10:18
【问题描述】:

我是一名 Python 初学者,我需要一些代码方面的帮助。附件是我一直在处理的代码。目的是取一个像 305.67 这样的美元金额,并将该数字转换为像“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

【问题讨论】:

标签: python formatting format numerical


【解决方案1】:

使用elif 代替

else:
if  

这是快速修复。 For, part to handle 11-19, before

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]

【讨论】:

  • 我在 之后怎么说,当一个数字是11 时,它会返回“ELEVEN”等等?
  • 就像在你的声明中一样,使用if amount==11: word="Eleven" 等等,但是开发人员有一个很好的解决方案,你可以看看它并了解代码实际上在做什么,并阅读以赛亚的漂亮 cmets,我的回答是不是一个完整的解决方案,而是一个快速指南。
  • 感谢您的意见!
【解决方案2】:

好的...这里有很多问题。让我们从一些基本的语法开始。

首先,您需要回过头来看看格式化在 Python 中是如何工作的。它在四个空格中工​​作,如果您使用正确的处理器,则相当于“制表符”。当您定义一个函数时,该函数中包含的所有内容都应该至少有 4 个空格。在你的函数“getWordForTwoDigits()”中你有这个:

def getWordForTwoDigits(amount):
...
return ...

那个返回应该是四个空格(顺便说一句,你对你的两个函数都这样做了)。

其次,您的 else-if 结构是古怪且不需要的。而不是你现在拥有的,只需这样做:

if TensAmount == 1:
    do something
elif TensAmount == 2:
    do something different

然后只需添加更多的 'elif's 到 9。

还有,你说

singlesWord = getWordForDigit(int(amount)%10)

但你从不定义那个函数。

对于如何区分 196 和 97 的实际问题,请尝试以下操作:

def splitter(num):
    sep=str(num).rsplit('.', 1)
    return sep

'rsplit' 基本上根据第一个参数中的字符将字符串分成两部分。它返回一个列表 ["partone", "parttwo"],因此您需要提取列表的两部分并将它们转换回主代码中的整数。

希望对您有所帮助!

【讨论】:

    【解决方案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
    

    请注意,它适用于整数。然而,将浮点数分成两个整数部分是微不足道的。

    【讨论】:

    • 虽然这可能是最正确的答案,但我不能使用它,因为一些概念还没有被教授。谢谢您的意见!我尊重伟大的努力!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多