0

我需要帮助保持空间。我有凯撒密码功能,但我希望它保留空间并且无法弄清楚如何做到这一点。

sentence = raw_input("Please enter a sentence : ").lower()
newString = ''
validLetters = "abcdefghijklmnopqrstuvwxyz"
space = [ ]
for char in sentence:
    if char in validLetters or char in space:
        newString += char
        shift = input("Please enter your shift : ")
        resulta = []
for ch in newString:
    x = ord(ch)
    x = x + shift
    resulta.append(chr(x if 97 <= x <= 122 else 96 + x % 122))
print sentence
print("")
print("Your encryption is :")
print("")
print ''.join(resulta)

它输出正确的代码,但删除了空格。如何保留加密空间?

4

3 回答 3

6
>>> key = string.ascii_lowercase
>>> rotation = 13
>>> tab = string.maketrans(key,key[rotation:]+key[:rotation])
>>> "hello world".translate(tab)
'uryyb jbeyq'
于 2013-10-30T17:56:18.787 回答
1

如果您不想加密空格,这对我有用:

sentence = raw_input("Please enter a sentence : ").lower()
newString = ''
validLetters = "abcdefghijklmnopqrstuvwxyz " #adding whitespace to valid chars...
space = []
for char in sentence:
    if char in validLetters or char in space:
        newString += char
shift = input("Please enter your shift : ")
resulta = []
for ch in newString:
    x = ord(ch)
    x = x+shift
    # special case for whitespace...
    resulta.append(chr(x if 97 <= x <= 122 else 96+x%122) if ch != ' ' else ch)
print sentence
print("")
print("Your encryption is :")
print("")
print ''.join(resulta)

有点短,但仍然是你的方式:

sentence = raw_input("Please enter a sentence : ").lower()
shift = input("Please enter your shift : ")

validLetters = map(chr, range(97, 123))
validLetters.append(' ')

tmp = [ord(ch)+shift for ch in sentence if ch in validLetters]
resulta = [chr(x if 97 <= x <= 122 else 96+x%122) if x != ord(' ')+shift else ' ' for x in tmp]

print sentence
print
print("Your encryption is :")
print
print ''.join(resulta)
于 2013-10-30T18:21:25.983 回答
0

Bibliography:
Kid Snippets: "Math Class" (Imagined by Kids) - YouTube http://youtu.be/KdxEAt91D7k
Mary Had A Little Lamb Nursery Rhyme With Lyrics - YouTube http://youtu.be/CkRdvGmcCBE
Mary Had a Little Lamb - Wikipedia, the free encyclopedia http://goo.gl/FNEuyd

Python source code:

Note: working for negative shift numbers also
Note: if reverse shift then we do encode - decode message
Note: preserving spaces also

small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
upper_chars = [item.upper() for item in small_chars]

def encode_chr(chr_item, is_upper_case):
    '''
    Cipher each chr_item.
    '''
    # setting orig and end order.
    if is_upper_case:
        orig_ord = ord('A')
        end_ord  = ord('Z')
    else:
        orig_ord = ord('a')
        end_ord  = ord('z')

    # calculating shift    
    temp_ord = ord(chr_item)+shift
    # calculating offset order with modulo.
    # char is after end_ord, calculating offset
    num_of_chars = 26
    offset_ord = (temp_ord - end_ord - 1)%num_of_chars
    return chr(orig_ord + offset_ord)

# enable while loop to repeat until status not 'y'
status = 'y'
while status == 'y':
    # enter word to cipher.
    word = raw_input("Word: ")
    # enter char shift
    shift = input("Shift: ")
    print

    # create cipher list variable
    cipher = list()
    # loop trough each char in word
    for chr_item in word:
        # encode just letters.
        # replace non-alfa with underscore: "_"
        if chr_item in upper_chars or chr_item in small_chars:
            # set is_uppser_case to True for upper case chars.
            is_upper_case = (chr_item in upper_chars) and True
            # cipher char.
            temp_chr = encode_chr(chr_item, is_upper_case)
            # append ciphered char to list
            cipher.append(temp_chr)
        elif chr_item is ' ':
            cipher.append(chr_item)
        else:
            cipher.append('_')

    # print word
    print word
    # print ciphered word
    print ''.join(cipher)

    # repeat again for another word?
    status = raw_input("Repeat? [y|n]: ")
    print

Test cases:

>>> 
Word: aAzZ!@
Shift: 1

aAzZ!@
bBaA__
Repeat? [y|n]: y

Word: aAzZ@!
Shift: -1

aAzZ@!
zZyY__
Repeat? [y|n]: y

Word: aAzZ@$
Shift: 27

aAzZ@$
bBaA__
Repeat? [y|n]: y

Word: aAzZ%^
Shift: -27

aAzZ%^
zZyY__
Repeat? [y|n]: n

>>> 

Output:

Note: if reverse shift then we do encode - decode message

>>>
Word: "Mary Had a Little    Lamb"
Shift: 1

"Mary Had a Little    Lamb"
_Nbsz Ibe b Mjuumf    Mbnc_
Repeat? [y|n]: y

Word: _Nbsz Ibe b Mjuumf    Mbnc_
Shift: -1

_Nbsz Ibe b Mjuumf    Mbnc_
_Mary Had a Little    Lamb_
Repeat? [y|n]: n

>>>
于 2014-11-18T07:30:58.337 回答