0

所以我的家庭作业是编写一个执行 ROTn 编码的程序。意思是将一串字母按值 n 移动。例如: if n = 2, then"a""c"在编码时。我们应该将移位后的字符串存储在一个变量中。

所以我刚刚开始这个讲座,所以我们还没有学到很多关于python的东西。这个问题应该是可以解决的,不需要导入东西。

所以我的想法是单独移动每个字母,然后将其存储为一个数组。然后我可以将它作为字符串输出print(''.join(my_array)。但是对于自动校正系统,移位的字符串应该存储在一个变量中,这给我带来了问题。我不知道怎么做。

if __name__ == "__main__":
    plain_text = "abc"
    shift_by = 1

# perform a ROTn encoding

plain_text = input("Please enter a password: ")
shift_by = int(input("Enter a value to shift the password: "))

store_shift = []
x = 0

for n in plain_text:
    if n.isalpha():
        n = chr(ord(n) + shift_by)
        store_shift.append(n)
        x += 1

encoded = ... #here should be the shifted string
print(''.join(store_shift)

请忽略我的错误变量名。随意纠正文体错误等。

总结一下; 我无法将变量中的字母数组作为字符串存储。例如; array["a", "b", "c"]应该存储在variable = abc(作为字符串)

4

1 回答 1

1

将数组转换为列表,然后使用:

''.join(some_list)

例如:

some_list = ['a','b','c']
some_string = ''.join(some_list)
print(some_string)
#'abc'
于 2019-10-05T13:06:01.163 回答