You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python数字转英文单词代码无输出问题排查及优化建议

问题排查与修复

原代码的核心问题

  • 类型不匹配:处理十位(x=2/5/8)时,用字符串n[0:2]去匹配OnesTensCase的整数键,永远匹配失败,导致这部分逻辑完全走不通。
  • 循环边界错误:循环条件x > (-1)会让x降到0甚至-1,此时10**(x-1)是0.1、0.01这类小数,整数除以小数得到浮点数,后续字典访问会出错且无意义。
  • 逻辑遗漏:当十位数字是1时(比如12的十位是1),y=1不在Ortyplanename的键中,这部分情况完全没处理,直接跳过。
  • 无效代码:str(y)这行没有赋值给任何变量,纯粹无效。
  • 多余的"And":无论是否需要都添加"And ",导致输出冗余。

修复后的代码

Dictcom = {1:'One ',2:'Two ',3:'Three ',4:'Four ',5:'Five ',6:'Six ',7:'Seven ',8:'Eight ',9:'Nine '}
Placename = {3:'Thousand ',6:'Million ',9:'Billion '}
Ortyplanename = {2:'Twenty ',3:'Thirty ',4:'Forty ',5:'Fifty ',6:'Sixty ',7:'Seventy ',8:'Eighty ',9:'Ninety '}
OnesTensCase = {10:'Ten ',11:'Eleven ',12:'Twelve ',13:'Thirteen ',14:'Fourteen ',15:'Fifteen ',16:'Sixteen ',17:'Seventeen ',18:'Eighteen ',19:'Nineteen '}

n_input = input("Enter a number less than 11 digits: ")
n = int(n_input)
x = len(n_input)
S = ''

while x > 0:
    # 处理当前最高位的分组
    if x in [3,6,9]:  # 千、百万、十亿的位置
        y = n // 10**(x-1)
        if y != 0:
            S += Dictcom[y]
            S += 'Hundred '
            # 检查后面是否有非零数字,再添加And
            if n % 10**(x-1) != 0:
                S += 'And '
        n = n % 10**(x-1)
        x -= 1
    elif x in [2,5,8]:  # 十位位置
        two_digits = n // 10**(x-2)
        if two_digits in OnesTensCase:
            S += OnesTensCase[two_digits]
            n = n % 10**(x-2)
            x -= 2
            continue
        y = n // 10**(x-1)
        if y != 0:
            S += Ortyplanename[y]
        n = n % 10**(x-1)
        x -= 1
    else:  # 个位位置
        y = n // 10**(x-1)
        if y != 0:
            S += Dictcom[y]
        # 添加对应的量级词(千、百万等)
        if (x) in Placename:
            S += Placename[x]
        n = n % 10**(x-1)
        x -= 1

# 去除末尾多余空格
S = S.strip()
print(S)

更优实现思路(分块处理)

数字转英文通常按三位一组拆分(个、千、百万、十亿),每组内统一处理成“几百几十几”,再加上对应的量级词,逻辑更清晰,不易出错:

def number_to_words(n):
    if n == 0:
        return "Zero"
    
    ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
    teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
    tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
    scales = ["", "Thousand", "Million", "Billion"]
    
    def convert_three_digits(num):
        word = ""
        hundreds = num // 100
        remainder = num % 100
        
        if hundreds != 0:
            word += ones[hundreds] + " Hundred"
            if remainder != 0:
                word += " And "
        
        if remainder >= 10 and remainder < 20:
            word += teens[remainder - 10]
        else:
            ten = remainder // 10
            one = remainder % 10
            if ten != 0:
                word += tens[ten]
                if one != 0:
                    word += " " + ones[one]
            elif one != 0:
                word += ones[one]
        return word.strip()
    
    words = []
    scale_index = 0
    
    while n > 0:
        three_digits = n % 1000
        if three_digits != 0:
            chunk = convert_three_digits(three_digits)
            if scale_index > 0:
                chunk += " " + scales[scale_index]
            words.insert(0, chunk)
        n = n // 1000
        scale_index += 1
    
    return " ".join(words)

# 测试
n = int(input("Enter a number less than 11 digits: "))
print(number_to_words(n))

内容的提问来源于stack exchange,提问作者jazl peak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:01:14