Python 为什么我的代码会删除替换代码中的999?

Python 为什么我的代码会删除替换代码中的999?,python,for-loop,encode,str-replace,string-substitution,Python,For Loop,Encode,Str Replace,String Substitution,我有下面的代码,用999替换所有标点符号,用数字位置替换所有字母。我已经包括了确认标点符号被替换的打印声明。但是,我似乎用剩余的代码替换了其他字符 import string def encode(text): punct = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' for x in text.lower(): if x in punct: text = text.replac

我有下面的代码,用999替换所有标点符号,用数字位置替换所有字母。我已经包括了确认标点符号被替换的打印声明。但是,我似乎用剩余的代码替换了其他字符

import string
def encode(text):
    punct = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    for x in text.lower(): 
        if x in punct: 
            text = text.replace(x, ".999")
            print(text)
        nums = [str(ord(x) - 96) 
                for x in text.lower()
                    if x >= 'a' and x <= 'z'
                    ]
    return ".".join(nums)
print(encode(str(input("Enter Text: "))))
导入字符串
def编码(文本):
点='''''!()-[]{};:'"\,./?@#$%^&*_~'''
对于text.lower()中的x:
如果x在点中:
text=text.replace(x,“.999”)
打印(文本)
nums=[str(ord(x)-96)
对于text.lower()中的x

如果x>='a'和x否,则此处有两个独立的逻辑“故事”。一个用
999
替换标点符号。另一个过滤掉所有字母,并构建其字母位置的独立列表

    nums = [str(ord(x) - 96) 
            for x in text.lower()
                if x >= 'a' and x <= 'z'
                ]
return ".".join(nums)
打印输出(编码(“[hello]”)

现在我们可以应用整个编码过程:我们需要一个字符串,将
“.
放在值的字符串表示形式之间。这很简单:

def encode(text):
    return '.'.join(str(encode_char(c)) for c in text)

您能否至少给出一个输入、接收输出和预期输出的实际示例?我不知道我应该在这里查找什么问题。例如,Morning!应该是(13.15.18.14.9.14.7.999)试着想出一个单独的函数,给定输入的一个字符,它会告诉你它应该表示的整数值。然后试着使用这个单独的函数一次创建你想要的文本。顺便说一句:你不需要
导入string
就可以使用string方法,但你可能应该使用它来实现这个目的它提供的静态字符串,例如
string.parantion
,它等于
'!“\$%&\'()*+,-./:;?@[\]^.
{124;}~”
。此外,
input`已经返回了一个字符串;在结果上使用
str
,它没有任何作用。
..9.9.9.8.5.12.12.15...9.9.9
    nums = [str(ord(x) - 96) 
            for x in text.lower()
                if x >= 'a' and x <= 'z'
                ]
def encode_char(c):
    if c in '''!()-[]{};:'"\,<>./?@#$%^&*_~''':
        return 999
    if 'a' <= c.lower() <= 'z':
        return ord(c) - 96
    # You should think about what to do in other cases!
    # In particular, you don't want digit symbols 1 through 9 to be
    # confused with letters A through I.
    # So I leave the rest up to you, depending on your requirements.
def encode(text):
    return '.'.join(str(encode_char(c)) for c in text)