第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python中的Caesar Cipher函數(shù)

Python中的Caesar Cipher函數(shù)

湖上湖 2019-09-03 16:13:10
我正在嘗試在Python中創(chuàng)建一個(gè)簡(jiǎn)單的Caesar Cipher函數(shù),它根據(jù)用戶(hù)的輸入移動(dòng)字母,并在最后創(chuàng)建一個(gè)最終的新字符串。唯一的問(wèn)題是最終的密文只顯示最后一個(gè)移位的字符,而不是一個(gè)包含所有移位字符的整個(gè)字符串。這是我的代碼:plainText = raw_input("What is your plaintext? ")shift = int(raw_input("What is your shift? "))def caesar(plainText, shift):     for ch in plainText:        if ch.isalpha():            stayInAlphabet = ord(ch) + shift             if stayInAlphabet > ord('z'):                stayInAlphabet -= 26            finalLetter = chr(stayInAlphabet)        cipherText = ""        cipherText += finalLetter    print "Your ciphertext is: ", cipherText    return cipherTextcaesar(plainText, shift)
查看完整描述

3 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

我意識(shí)到這個(gè)答案并沒(méi)有真正回答你的問(wèn)題,但我認(rèn)為無(wú)論如何它都是有用的。以下是使用字符串方法實(shí)現(xiàn)caesar密碼的另一種方法:


def caesar(plaintext, shift):

    alphabet = string.ascii_lowercase

    shifted_alphabet = alphabet[shift:] + alphabet[:shift]

    table = string.maketrans(alphabet, shifted_alphabet)

    return plaintext.translate(table)

實(shí)際上,由于字符串方法是在C中實(shí)現(xiàn)的,因此我們將看到此版本的性能提升。這就是我認(rèn)為的“pythonic”方式。



查看完整回答
反對(duì) 回復(fù) 2019-09-03
?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

你需要cipherText = ""在for循環(huán)開(kāi)始之前移動(dòng)。你每次循環(huán)都要重置它。


def caesar(plainText, shift): 

  cipherText = ""

  for ch in plainText:

    if ch.isalpha():

      stayInAlphabet = ord(ch) + shift 

      if stayInAlphabet > ord('z'):

        stayInAlphabet -= 26

      finalLetter = chr(stayInAlphabet)

      cipherText += finalLetter

  print "Your ciphertext is: ", cipherText

  return cipherText


查看完整回答
反對(duì) 回復(fù) 2019-09-03
  • 3 回答
  • 0 關(guān)注
  • 1280 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)