简体   繁体   English

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

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

I wanted to write a ROT 13 Encoder in python.我想用 python 编写一个 ROT 13 编码器。 I have never used python before.我以前从未使用过python。 The encoder i want should I tried writing a script but its only able to do the basic ROT 13. Here is one of the script i have been using我想要的编码器是否应该尝试编写脚本,但它只能执行基本的 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)

You may have heard of Zen of Python, which is printed when you import this .你可能听说过 Python 的 Zen,它在你import this . It actually uses the rot13 encoding.它实际上使用了 rot13 编码。 You may see its source in official cPython git here您可以在此处的官方 cPython git 中看到它的源代码

You are almost there, you only forgot to change to upper case:你快到了,你只是忘了改成大写:

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)

Convert the input to uppercase and your script will work fine.将输入转换为大写,您的脚本将正常工作。 You could do it while taking input with the upper() function:您可以在使用upper()函数获取输入时执行此操作:

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

Hope it helps.希望能帮助到你。

You can also use this simple script:您还可以使用这个简单的脚本:

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