为什么下面的代码不能正确输出对应的字母呢?
Why does the following code not output the corresponding letters correctly?
为什么下面的代码没有正确输出对应的字母?
输出number to letter is['\x03', '\x03']
final_numbers = [3, 3]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=chr(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))
chr(3)
returnsASCII码为3的字符(即'\x03')
将数字转换为字符串 (int-str)
你必须做 str()
而不是 chr()
所以这样做:
final_numbers = [3, 3]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=str(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))
或者如果你想这样做 chr()
(尝试猜测结果是什么)
final_numbers = [115, 116, 97, 99, 107, 32, 111, 118, 101, 114, 102, 108, 111, 119]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=chr(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))
为什么下面的代码没有正确输出对应的字母?
输出number to letter is['\x03', '\x03']
final_numbers = [3, 3]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=chr(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))
chr(3)
returnsASCII码为3的字符(即'\x03')
将数字转换为字符串 (int-str)
你必须做 str()
而不是 chr()
所以这样做:
final_numbers = [3, 3]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=str(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))
或者如果你想这样做 chr()
(尝试猜测结果是什么)
final_numbers = [115, 116, 97, 99, 107, 32, 111, 118, 101, 114, 102, 108, 111, 119]
final_letters = []
for i in range(len(final_numbers)):
print(i)
number_to_letter=chr(final_numbers[i])
final_letters.append(number_to_letter)
print ("number to letter is" + str(final_letters))