這個加密文本的程序需要以下用戶輸入:
1.要加密的文本text
2.正整數prime而有效的prime值包括:1,3,5,7,9,11,15,17,19,21,23,25
3.正整數number和字母移動位置n,其中n由下式給出:
(primer ? position of each letter + number)
例如,如果primer = 1,number = 1,則’A’將變為’B’
則:(1 * 0 ['A’的字母位置] + 1)= 1 ['B’的字母位置]。
- 思路是首先由用戶輸入文本和兩個加密值,先判斷是否輸入符合要求
def check(text, prime, positiveInt):
if not text.isalpha():
print("text are not right!\n")
prime = str(prime)
if not prime.isdigit():
print("prime are not right!\n")
positiveInt = str(positiveInt)
if not positiveInt.isdigit():
print("positive number are not right!\n")
- 然后檢驗其字母是大寫還是小寫然后匹配處位置作下標再分別代入公式后結果用append()函數加入列表即可:
def transform(text, prime, positiveInt):
PrimeArea = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
lower_str = 'abcdefghigklmnopqrstuvwxyz'
upper_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ'
str = []
if prime not in PrimeArea:
print("please enter right prime!!!\n")
else:
for item in range(len(text)):
if text[item].isupper():
for i in range(26):
if text[item] == upper_str[i]:
bposition = ((prime * i) + positiveInt) % 26
str.append(upper_str[bposition])
elif text[item].islower():
for i in range(26):
if text[item] == lower_str[i]:
bposition = ((prime * i) + positiveInt) % 26
str.append(lower_str[bposition])
else:
print("error text!!!\n")
print("".join(str))
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
