Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查字符串中的特定字母?_Python_Python 3.x_For Loop_Encryption_Python Idle - Fatal编程技术网

Python 如何检查字符串中的特定字母?

Python 如何检查字符串中的特定字母?,python,python-3.x,for-loop,encryption,python-idle,Python,Python 3.x,For Loop,Encryption,Python Idle,我的Python3加密程序有问题。我编写的代码使用比较来检查userMessage中的某些字母,使用if语句和for循环。我遇到的问题是,在加密输出的每个字母之间都会弹出字母表。例如,如果输出应该是amxer,则它是aAmBxCeDr。另外,检查vwxyz的第二条if语句在ASCII中向右滚动vwxyz5次加密的符号之间弹出vwxyz。第二个if语句是将这些符号更改为字母abcde #Start #variables encryptedMessage="" userMessage = input

我的Python3加密程序有问题。我编写的代码使用比较来检查
userMessage
中的某些字母,使用
if
语句和
for
循环。我遇到的问题是,在加密输出的每个字母之间都会弹出字母表。例如,如果输出应该是
amxer
,则它是
aAmBxCeDr
。另外,检查
vwxyz
的第二条
if
语句在ASCII中向右滚动
vwxyz
5次加密的符号之间弹出
vwxyz
。第二个
if
语句是将这些符号更改为字母
abcde

#Start
#variables
encryptedMessage=""
userMessage = input("your message here: ")
userMessage =userMessage.lower()
shift=5
#function
def step2():
    #for loop
    for character in userMessage:
        #5:50AM February 24th, 2020
        #looking for global variable
        global encryptedMessage
        if character== "a"or"b"or"c"or"d"or"e"or"f"or"g"or"h"or"i"or"j"or"k"or"l"or"m"or"n"or"o"or"p"or"q"or"r"or"s"or"t"or"u":
            eN = ord(character)
            eN+= shift
            encryptedMessage+=chr(eN)  
        #checking for letters that go to random symbols
        if character=="v"or"w"or"x"or"y"or"z":

            if character=="v": character=="a"
            if character=="w": character=="b"
            if character=="x": character=="c"
            if character=="y": character=="d"
            if character=="z": character=="e"
            encryptedMessage+=character

step2()
print(userMessage)
print(encryptedMessage)
我的代码的输出为:

your message here: abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
fagbhcidjekflgmhniojpkqlrmsntoupvqwrxsytzu{v|w}x~yz
>>> 

您使用的语法无效
character=='a'或'b'
(character=='a')或'b'相同,后者始终为
True
,因为
'b'不是None`

如果你想使用多个相等,你必须把它们写成
character=='a'或character=='b'或…

或者,更好的解决方案(如下所示)是使用
set()
in
操作符,它检查字符是否在集合中,如果找到则返回True

encryptedMessage=""
userMessage = input("you'r message here: ")
userMessage =userMessage.lower()
shift=5
#function
FIRST= set(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u"])
SECOND = set(["v","w","x","y","z"])
def step2():
    #for loop
    for character in userMessage:
        #5:50AM February 24th, 2020
        #looking for global variable
        global encryptedMessage
        if character in FIRST:
            eN = ord(character)
            eN+= shift
            encryptedMessage+=chr(eN)  
        #checking for letters that go to random symbols
        if character in SECOND:
            if character=="v": character=="a"
            if character=="w": character=="b"
            if character=="x": character=="c"
            if character=="y": character=="d"
            if character=="z": character=="e"
            encryptedMessage+=character

step2()
print(userMessage)
print(encryptedMessage)
因为我现在才意识到这是一个简单的旋转密码,这里是另一个如何实现的例子

考虑到A为0,Z为25,您可以使用模块化算法进行简单的旋转,如
加密的_char=(char+shift)%26
。在我们的例子中,字母
'a'
'z'
不在数字1上,但我们可以通过减去然后添加起始值来轻松解决这个问题

A = ord('a')
def lowercase_rotate_cypher( message, shift):
    accumulator = []
    for character in message:
        c = A + ( ( ord(character) + shift - A ) % 26)
        accumulator.append( chr(c) )
    return ''.join(accumulator)

在您的情况下,
shift=5
和消息从输入端传递

你的比较不起作用
character=='a'或'b'
(character=='a')或'b'
相同,这总是正确的,因为
b不是None
。我建议还有两个改进:1)不要使用全局变量-将输入字符串作为参数传递给函数,并从函数返回结果;2) 利用
ord(“v”)-ord(“a”)是21这一事实。直到现在才意识到这只是一个简单的移位加密。我添加了小写的rot函数示例。