gpt4 book ai didi

python - 如何在Python中将int打印为单词seq

转载 作者:行者123 更新时间:2023-11-28 21:31:14 25 4
gpt4 key购买 nike

我有一个包含单词数字的表格

table = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

用户应该在输入中写一个数字:

number = input()

现在我需要以这种方式创建打印单词表示的函数(例如 242):

> 242 - two four two

我编写了该代码,但它无法正常工作(索引或打印列表有问题)。我怎样才能以最简单的方式实现这个功能?

我尝试过的代码:

table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
number = input()

for x in range(len(table)):
print(table[number[x]], end=' ')

最佳答案

试试这个:

table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
number = input()

# loop over the input string to get number by number
for x in number:
# now get the string representation from its index in table
print(table[int(x)], end=' ')

输出:

684
six eight four

586493
five eight six four nine three

关于python - 如何在Python中将int打印为单词seq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58574287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com