以简单的方式将数字转换为单词

时间:2016-03-12 09:41:14

标签: python-2.7 python-3.x

写一个以num作为参数的函数num_to_word,该函数应该将参数转换为string。例如,num_to_word(234)应返回两个三四。  请帮帮我。

value = round(pow(10,value));

这是我得到的错误

def num_to_word(num):
    result = ''
    literals = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    num = num.str()
    for i in range (0, len(num)):
        result += literals[num[i]] + ' '
    return result;

num_to_word(1390)

2 个答案:

答案 0 :(得分:1)

你可以做到这一点,对我来说效果很好。

Predicate<Employee> matcher = employee -> employee.getSalary() == 1400d;
Optional<Employee> opt = data.stream().filter(matcher).findAny();

combobox.setValue(opt.orElse(null)); // set found employee or null, if none was found.

答案 1 :(得分:0)

错误很少。我修改了你的代码,你应该好好去。

int.str()不是python函数,当你将int转换为字符串时,literal[num[i]]将是literal[string],这就是错误所在的位置。您需要在通话时打印以查看结果。

def num_to_word(num):
    result = ''
    literals = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    num = str(num)
    for i in range(len(num)):
        result += literals[int(num[i])] + ' '
    return result

print num_to_word(1390)