Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

python - how to fix my Caesar Cipher function and how the % loop over within the length?

realText = input('please enter a string: ')
step = int(input('please enter step for shifting'))

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []
    
    uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    for eachLetter in realText:
        if eachLetter in uppercase:
            index = uppercase.index(eachLetter)
            crypting = (index + step) % len(uppercase)
            cryptText.append(crypting)  #list of each crpting index
            newLetter = uppercase[crypting]
            outText.append(newLetter)
        elif eachLetter in lowercase:
            index = lowercase.index(eachLetter)
            crypting = (index + step) % len(lowercase)
            cryptText.append(crypting)
            newLetter = lowercase[crypting]
            outText.append(newLetter)
    print((' ').join(outText))
caesar_encrypt(realText, step)

Input:

BEWARE THE IDES OF MARCH 
step: 13

Unexpected output:

O R J N E R G U R V Q R F B S Z N E P U

Expected output:

ORJNER GUR VQRF BS ZNEPU

As my code is shown above, how can I fix this code to get my desired output? and I don't understand how the modulus operators are able to use to loop over the length of a sequence and isn't it for the reminder? can anyone explain more details about that? will be very appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should add else in your code and join finally list with '' as below

realText = input('please enter a string: ')
step = int(input('please enter step for shifting'))

def caesar_encrypt(realText, step):
    outText = []
    cryptText = []
    
    uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    for eachLetter in realText:
        if eachLetter in uppercase:
            index = uppercase.index(eachLetter)
            crypting = (index + step) % len(uppercase)
            cryptText.append(crypting)  #list of each crpting index
            newLetter = uppercase[crypting]
            outText.append(newLetter)
        elif eachLetter in lowercase:
            index = lowercase.index(eachLetter)
            crypting = (index + step) % len(lowercase)
            cryptText.append(crypting)
            newLetter = lowercase[crypting]
            outText.append(newLetter)
        else:
            outText.append(eachLetter)
    print(''.join(outText))
caesar_encrypt(realText, step)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...