Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ord()应为字符错误_Python_Unicode - Fatal编程技术网

Python ord()应为字符错误

Python ord()应为字符错误,python,unicode,Python,Unicode,我正在编写一个程序,对用户选择的文本进行编码和解码。当我输入unicode代码返回字符时,我的解码器功能似乎工作正常,但我的编码器功能不工作 简单地说,我想实现的是: 询问用户是否要编码或解码 如果他们想编码,我想收集一串短语,直到用户输入‘done’,将它们附加到列表中,然后使用我的encode函数对它们进行编码,最后打印出编码列表 如果他们想要解码,我想收集一系列实际上是unicode代码的整数,直到用户输入'done',将它们附加到列表2中,然后使用我的解码函数对它们进行解码并打印出解码列

我正在编写一个程序,对用户选择的文本进行编码和解码。当我输入unicode代码返回字符时,我的解码器功能似乎工作正常,但我的编码器功能不工作

简单地说,我想实现的是:

  • 询问用户是否要编码或解码
  • 如果他们想编码,我想收集一串短语,直到用户输入‘done’,将它们附加到列表中,然后使用我的encode函数对它们进行编码,最后打印出编码列表
  • 如果他们想要解码,我想收集一系列实际上是unicode代码的整数,直到用户输入'done',将它们附加到列表2中,然后使用我的解码函数对它们进行解码并打印出解码列表
  • 这是我的密码

    def encode(character):
        code = ord(character)
        new_code = code * 4 + 10 // 2
    
        return new_code
    
    def decode(character):
        code2 = character * 2 - 10 // 4
        new_code2 = chr(code2)
    
        return new_code2
    
    def main():
        encoded_list = []
        decoded_list = []
    
        choice = input('Hello! Do you want to encode or decode? ').lower()
    
        if choice == 'encode':
            text = input('Enter text to encode or "done": ')
            while text != 'done':
                encoded = encode(text)
                encoded_list.append(encoded)
                text = input('Enter text to encode or "done": ')
    
            if text == 'done':
                print(encoded_list)
    
        elif choice == 'decode':
            text2 = input('Enter characters to decode or "done": ')
            while text2 != 'done':
                text2 = int(text2)
                decoded = decode(text2)
                decoded_list.append(decoded)
                text2 = input('Enter characters to decode or "done": ')
    
            if text2 == 'done':
                print(decoded_list)
    
        else:
            print('Please enter a valid response')
    
    main()
    

    非常感谢

    主要问题是必须分别对字符串的每个字符进行编码。
    ord
    函数只能接受一个字符,因此不能:

    encoded = encode(text)
    
    你想要:

    encoded = ""
    for char in text:
        encoded += encode(text)
    
    另外(这与您得到的错误无关),您忘记了说明操作顺序。您将
    code*4+10//2
    替换为
    (code*4+10)//2
    ,因此该代码实际上相当于
    code*4+5
    。您对解码执行了类似的操作。

    您需要对字符串的每个字符使用ord,而不是一次对整个字符串使用ord:

    def encode(character):
        code = map(ord,character)
        new_code = [(x * 4 + 10) // 2 for x in code]
        return new_code
    
    def decode(character):
        code2 = [(x * 2 - 10) // 4 for x in character]
        new_code2 = "".join([chr(x) for x in code2])
        return new_code2
    

    你读代码了吗?读代码。通读一遍,ord只需要一个字符