小编Adm*_*yro的帖子

将变量整数转换为列表中的一位数字单词形式

创建一个名为list_numbers_in_words(...)的函数,该函数接收一个正整数作为参数,并返回一个列表,该列表包含与每个数字相对应的单词,其顺序与数字中出现的数字相同。例如,以下代码片段:

res = list_numbers_in_words(5438)
print(res)
Run Code Online (Sandbox Code Playgroud)

应该产生输出:

['five', 'four', 'three', 'eight']
Run Code Online (Sandbox Code Playgroud)

代码无效:

def list_numbers_in_words(num):
    l = list(num);
    single_digits = ["zero", "one", "two", "three",  
                     "four", "five", "six", "seven",  
                     "eight", "nine"]; 
    if (l==1):  
        print(single_digits[ord(num[0]) - '0']); 
        return; 
    x = 0; 
    while (x < len(num)):
     if (l >= 3): 
        if (ord(num[x]) - 48 != 0): 
                print(single_digits[ord(num[x]) - 48],  
                                           end = " "); 
                print(tens_power[l - 3], end = " ");
        l -= 1; 
     else: 
        if (ord(num[x]) - 48 == 1):  
                sum = (ord(num[x]) - 48 …
Run Code Online (Sandbox Code Playgroud)

python

-3
推荐指数
1
解决办法
188
查看次数

标签 统计

python ×1