Python凯撒密码保留空格 [英] Python Caesar Cipher keeping spaces

查看:173
本文介绍了Python凯撒密码保留空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助保留空格。我有凯撒密码功能,
,但我希望它保留空格,不知道该怎么做。

  sentence = raw_input(请输入句子:).lower()
newString =''
validLetters =abcdefghijklmnopqrstuvwxyz
space = []
句子中的char:
如果char in validLetters或char in space:
newString + = char
shift = input(请输入您的班级:)
resulta = []
在newString中的ch:
x = ord(ch)
x = x + shift
resulta.append(chr(x if 97 <= x <= 122 else 96 + x
打印句子
print()
print(你的加密是:)
print()
print''。 (resulta)

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

解决方案

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

  sentence = raw_input(请输入句子:).lower()
newString =''
validLetters =abcdefghijklmnopqrstuvwxyz#将空格添加到有效的字符...
space = []
在句子中的char:
如果char in validLetters或char in space:
newString + = char
shift = input(请输入您的移位:)
resulta = []
for new in newString:
x = ord(ch)
x = x + shift
#空格的特殊情况...
resulta.append(chr(x if 97 <= x <= 122 else 96 + x%122)if ch!=''
打印句子
print()
print(你的加密是:)
print()
print''.join结果a

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

  sentence = raw_input(Please en $ a 

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

tmp = [ord(ch)+如果ch在validLetters中,ch的移位]
resulta = [chr(x if 97 <= x如果x!= ord('')+对于tmp中的x替换,则= 122 else 96 + x%122)

打印句
print
print (你的加密是:)
print
print''.join(resulta)


I need help keeping the spaces. I have the caesar cipher functioning, but I want it to keep the spaces and can't figure out how to do that.

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)

It outputs correct code but removes the spaces. How can I keep the spaces on encryption?

解决方案

If you don't want to encrypt the spaces, this works for me:

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)

a little bit shorter, but still your way:

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)

这篇关于Python凯撒密码保留空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆