【问题标题】:How to return an array of characters in cypher program (python3)如何在密码程序(python3)中返回一个字符数组
【发布时间】:2023-03-12 16:18:02
【问题描述】:

当输入例如“a”他返回“h”时,我编写了代码。但是,如果我想返回字符数组,例如如果输入“aa”,我如何使它工作 返回“hh”?

def input(s):
    for i in range(len(s)):
        ci = (ord(s[i])-90)%26+97
        s = "".join(chr(ci))
    return s 

【问题讨论】:

标签: python ascii chr ord


【解决方案1】:

切勿将内置名称用作input

l = []


def input_x(s):
    for i in s:
        i = (ord(i)-90)%26+97
        l.append(chr(i))
    s = ''.join(l)
    return s

【讨论】:

    【解决方案2】:

    您可以使用字符串来执行此操作。我的变量 finaloutput 是一个字符串,我将使用它来存储所有更新的字符。

    def foo(s):
        finaloutput = ''
        for i in s:
            finaloutput += chr((ord(i)-90)%26+97)
        return finaloutput
    

    此代码使用字符串连接将一系列字符加在一起。由于字符串是可迭代的,因此您可以使用上面显示的 for 循环来代替您使用的复杂循环。

    【讨论】:

    • 谢谢阿鲁纳夫。
    【解决方案3】:
    def input_x(s):
        result = ""
        for i in s:
            ci = (ord(i)-90)%26+ 97
            result += chr(ci)
        print(result)
    

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2014-12-18
      • 2020-03-18
      • 2015-02-03
      相关资源
      最近更新 更多