-1

お金の値を文字列値に変換するとします。例: 10675.78 は、「1 万 675 ドルと 78/100 ドル」になります。

私はこれを必要以上に複雑にしていると思います。助言がありますか?

def money(value):

    x = str(value)
    if len(x)>8:
        return ("Error. Value exceeds limit.")
    elif len(x)==0:
        return ("Error. Must enter a value.")
    for len(x) in range(0,7):
        for x[6:8]:
            a = x[6:8]
            print(a,'/100 dollars')

        for x[3:5]:
            b = x[4:6]
            if b = '10'
                tens = 'ten'
            elif b = '11'
                tens = 'eleven'
            elif b = '12'
                tens = 'twelve'
            elif b = '13'
                tens = 'thirteen'
            elif b = '14'
                tens = 'fourteen'
            elif b = '15'
                tens = 'fifteen'
            elif b = '16'
                tens = 'sixteen'
            elif b = '17'
                tens = 'seventeen'
            elif b = '18'
                tens = 'eighteen'
            elif b = '19'
                tens = 'nineteen'
            else:
                return false

        for x[4]:
            c = x[4]
            if b = true:
                fifth = ''  
            elif c = '9'
                fifth = 'nine'
            elif c = '8'
                fifth = 'eight'
            elif c = '7'
                fifth = 'seven'
            elif c = '6'
                fifth = 'six'
            elif c = '5'
                fifth = 'five'
            elif c = '4'
                fifth = 'four'
            elif c = '3'
                fifth = 'three'
            elif c = '2'
                fifth = 'two'
            elif c = '1'
                fifth = 'one'
            elif c = '0'
                fifth = ''

        for x[3]:
            d = x[3]
            if b = true:
                fourth = ''
            elif d = '9'
                fourth = "ninety "
            elif d = '8'
                fourth = "eighty "
            elif d = '7'
                fourth = "seventy "
            elif d = '6'
                fourth = "sixty "
            elif d = '5'
                fourth = "fifty "
            elif d = '4'
                fourth = "forty "
            elif d = '3'
                fourth = "thirty "
            elif d = '2'
                fourth = "twenty "
            elif d = '0'
                fourth = ''

        for x[2]:
            e = x[2]
            if e = '9'
                third = 'nine hundred '
            elif e = '8'
                third = 'eight hundred '
            elif e = '7'
                third = 'seven hundred '
            elif e = '6'
                third = 'six hundred '
            elif e = '5'
                third = 'five hundred '
            elif e = '4'
                third = 'four hundred '
            elif e = '3'
                third = 'three hundred '
            elif e = '2'
                third = 'two hundred '
            elif e = '1'
                third = 'one hundred '



        for x[0:2]:
            j = [0:2]
            if j = '10'
                second = 'ten thousand '
            elif j = '11'
                second = 'eleven thousand '
            elif j = '12'
                second = 'twelve thousand '
            elif j = '13'
                second = 'thirteen thousand '
            elif j = '14'
                second = 'fourteen thousand '
            elif j = '15'
                second = 'fifteen thousand '
            elif j = '16'
                second = 'sixteen thousand '
            elif j = '17'
                second = 'seventeen thousand '
            elif j = '18'
                second = 'eighteen thousand '
            elif j = '19'
                second = 'nineteen thousand'
            else:
                return false

        for x[1]:
            g = x[1]
            if j = true:
                second = ''
            elif g = '9'
                second = 'nine thousand '
            elif g = '8'
                second = 'eight thousand '
            elif g = '7'
                second = 'seven thousand '
            elif g = '6'
                second = 'six thousand '
            elif g = '5'
                second = 'five thousand '
            elif g = '4'
                second = 'four thousand '
            elif g = '3'
                second = 'three thousand '
            elif g = '2'
                second = 'two thousand '
            elif g = '1'
                second = 'one thousand'
            elif g = '0'
                second = ''

        for x[0]:
            h = x[0]
            if j = true:
                first = ''
            elif h = '9'
                first = 'ninety '
            elif h = '8'
                first = 'eighty '
            elif h = '7'
                first = 'seventy '
            elif h = '6'
                first = 'sixty '
            elif h = '5'
                first = 'fifty '
            elif h = '4'
                first = 'fourty '
            elif h = '3'
                first = 'thirty '
            elif h = '2'
                first = 'twenty '
4

1 に答える 1

1

forループの使用法は見当たりません。数値の各部分に対して、アクションを1回だけ繰り返します。

さらに、よりクリーンなコードのために、次のような複雑な if 構造を保存する辞書を使用します。

hDict = {'2':'twenty', '3':'thirty',...}

first=hDict[x[0]]

等々。

于 2013-10-16T19:47:55.527 に答える