我想把给定的电话号码转换成相应的字母
0 -> 'a'
1 -> 'b'
2 -> 'c' etc.
例如,数字210344222应转换为字符串“cbadeeccc”。 我知道我的报税表在最后是错误的,而这正是我被卡住的地方,所以请你解释一下我将如何取而代之地返回转换后的信件
def phone(x):
"""
>>> phone(22)
'cc'
>>> phone(1403)
'bead'
"""
result = ""
x = str(x)
for ch in x:
if x == 0:
print('a')
elif x == 1:
print('b')
elif x == 3:
print('c')
return result
有一个名为^{} in the ^{} package 的常量,可用于按您描述的方式将数字转换为字母,您可以使用该数字并在
ascii_lowercase
中获取该索引以获取字母您可以尝试使用内置的^{} 方法:
输出:
其中
chr(97)
返回'a'
,chr(98)
返回'b'
,依此类推,因此是int(i) + 97
位使用chr()和ord()并计算“a”+数字
相关问题 更多 >
编程相关推荐