繁体   English   中英

我想用 python 编写一个 ROT 13 编码器

[英]I want to write an ROT 13 Encoder in python

我想用 python 编写一个 ROT 13 编码器。 我以前从未使用过python。 我想要的编码器是否应该尝试编写脚本,但它只能执行基本的 ROT 13。这是我一直在使用的脚本之一

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
num_chars = len(alphabet)
rot_amt = 13

string_input = input('Enter a string: ')
string_output = ''

for curr_char in string_input:
    char_loc = alphabet.index(curr_char)
    new_loc = (char_loc + rot_amt) % num_chars
    string_output += alphabet[new_loc]

print(string_output)

你可能听说过 Python 的 Zen,它在你import this . 它实际上使用了 rot13 编码。 您可以在此处的官方 cPython git 中看到它的源代码

你快到了,你只是忘了改成大写:

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
num_chars = len(alphabet)
rot_amt = 13

string_input = input('Enter a string: ')
string_output = ''

for curr_char in string_input:
    char_loc = alphabet.index(curr_char.upper())
    new_loc = (char_loc + rot_amt) % num_chars
    string_output += alphabet[new_loc]

print(string_output)

将输入转换为大写,您的脚本将正常工作。 您可以在使用upper()函数获取输入时执行此操作:

string_input = input('Enter a string: ').upper()

希望能帮助到你。

您还可以使用这个简单的脚本:

import codecs
s  = input("Input your text: ")
os = codecs.encode( s, "rot13" )
print(os)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM