簡體   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