完成作业321

This commit is contained in:
2509165005
2026-04-21 11:25:12 +08:00
parent 4892ed88c3
commit ea4699acbc
3 changed files with 58 additions and 0 deletions

20
题目1.py Normal file
View File

@@ -0,0 +1,20 @@
word = "Hello"
print("1. 使用 ord() 函数:")
for char in word:
ascii_val = ord(char)
print(f" '{char}' -> {ascii_val}")
print("\n2. 使用 chr() 函数验证:")
ascii_value = 65
char_from_ascii = chr(ascii_value)
print(f" ASCII码 {ascii_value} -> 字符 '{char_from_ascii}'")
print("\n3. 完整转换验证:")
print(" 字符串 'Hello' 的ASCII码列表:")
ascii_list = [ord(c) for c in word]
print(f" {ascii_list}")
print(" 从ASCII码列表恢复字符串:")
recovered_word = ''.join(chr(code) for code in ascii_list)
print(f" {ascii_list} -> '{recovered_word}'")