Python 3.x Python vigenere不';不接受空格

Python 3.x Python vigenere不';不接受空格,python-3.x,for-loop,cycle,itertools,vigenere,Python 3.x,For Loop,Cycle,Itertools,Vigenere,我编写了一个python vigenere密码,它不接受空格,所以我添加了一些代码,现在它无法打印正确的输出 如果输入为“计算很有趣”,关键字为“gcse”,则输出应为“jrfubwbsn ll kbq”。在我的程序中,输出是“jrfubwbsn bx ins” 下面是a应该如何工作 代码如下: from itertools import cycle print("NOTE: The program does not encrpyt/decrypt numbers or ASCII chara

我编写了一个python vigenere密码,它不接受空格,所以我添加了一些代码,现在它无法打印正确的输出

如果输入为“计算很有趣”,关键字为“gcse”,则输出应为“jrfubwbsn ll kbq”。在我的程序中,输出是“jrfubwbsn bx ins”

下面是a应该如何工作

代码如下:

from itertools import cycle

print("NOTE: The program does not encrpyt/decrypt numbers or ASCII characters. These include punctuation marks. Trying to encrypt/decrypt these values will print an error and you will have to try again.\n")

def main():
    def encrypt():
        boolean = False
        while boolean == False:
            message = str(input("Input a message: "))
            keyword = input("Keyword: ")
            if message.isdigit() == True or keyword.isdigit() == True:
                print("Invalid data type! Please try again.\n")
            else:
                boolean = True

        keystream = cycle(keyword)
        encrypted_msg = ''
        for msg, key in zip(message, keystream):
            spaceChar = 0
            newMsg = ord(msg)
            newKey = ord(key)

            switch = True
            if newMsg == 32:
                encrypted_msg += " "
                switch = False
            else:
                pass

            if switch == True:
                if 64 < newMsg < 91:
                    newMsg -= 64
                elif 96 < newMsg < 123:
                    newMsg -= 96
                else:
                    pass

                if 64 < newKey < 91:
                    newKey -= 64
                elif 96 < newKey < 123:
                    newKey -= 96
                else:
                    pass

                newChar = newMsg + newKey + 96


                if newChar > 122:
                    newChar = newChar - 26
                elif 96 > newChar > 91:
                    newChar -= 26

                encrypted_msg += chr(newChar)
            else:
                pass



        print("Encrypted message: %s" %encrypted_msg)

    def decrypt():
        boolean = False
        while boolean == False:
            message = str(input("Input a message: "))
            keyword = input("Keyword: ")
            if message.isalpha() == True or keyword.isalpha() == True:
                boolean = True
            elif message.isalpha() == False:
                print("Invalid data type! Please try again.\n")
            else:
                pass

        keystream = cycle(keyword)
        decrypted_msg = ''
        for msg, key in zip(message, keystream):
            newMsg = ord(msg)
            newKey = ord(key)

            if 64 < newMsg < 91:
                newMsg -= 64
            elif 96 < newMsg < 123:
                newMsg -= 96
            else:
                pass

            if 64 < newKey < 91:
                newKey -= 64
            elif 96 < newKey < 123:
                newKey -= 96
            else:
                pass

            newMsg = ord(msg) - 96
            newKey = ord(key) - 96
            newChar = newMsg - newKey + 96


            if newChar > 122:
                newChar = newChar - 26
            elif 96 > newChar > 91:
                newChar -= 26


            decrypted_msg += chr(newChar)

        print("Decrypted message: %s" %decrypted_msg)

    def task2choice():
        boolean = False
        while boolean == False:
            choice = str(input("Encrypt or Decrypt. [e/d]: "))
            print(" ")
            if choice.lower() == 'e' or choice.lower() == 'd':
                boolean = True
            else:
                print("Please select 'e' or 'd'.")
        if choice.lower() == 'e':
            encrypt()
        elif choice.lower() == 'd':
            decrypt()
        else:
            pass
        print(' ')
    task2choice()

def again():
    again = input("Would you like to restart? [y/n]: ")
    return again.lower() == 'y'


while main() or again():
    pass
来自itertools导入周期的

打印(“注意:程序不加密/解密数字或ASCII字符。这些字符包括标点符号。尝试加密/解密这些值将打印错误,您必须重试。\n”)
def main():
def encrypt():
布尔=假
而布尔值==False:
message=str(输入(“输入消息:”)
关键字=输入(“关键字:”)
如果message.isdigit()==True或关键字.isdigit()==True:
打印(“无效数据类型!请重试。\n”)
其他:
布尔=真
keystream=循环(关键字)
加密消息=“”
对于msg,输入zip(消息、密钥流):
spaceChar=0
newMsg=ord(msg)
newKey=ord(key)
开关=真
如果newMsg==32:
加密的_msg+=“”
开关=假
其他:
通过
如果开关==真:
如果64122:
newChar=newChar-26
elif 96>newChar>91:
newChar-=26
加密的\u msg+=chr(newChar)
其他:
通过
打印(“加密邮件:%s”%Encrypted\u msg)
def decrypt():
布尔=假
而布尔值==False:
message=str(输入(“输入消息:”)
关键字=输入(“关键字:”)
如果message.isalpha()==True或关键字.isalpha()==True:
布尔=真
elif message.isalpha()==False:
打印(“无效数据类型!请重试。\n”)
其他:
通过
keystream=循环(关键字)
已解密的_msg=“”
对于msg,输入zip(消息、密钥流):
newMsg=ord(msg)
newKey=ord(key)
如果64122:
newChar=newChar-26
elif 96>newChar>91:
newChar-=26
解密的_msg+=chr(newChar)
打印(“解密邮件:%s”%Decrypted\u msg)
def task2choice():
布尔=假
而布尔值==False:
choice=str(输入(“加密或解密。[e/d]:”)
打印(“”)
如果choice.lower()=“e”或choice.lower()=“d”:
布尔=真
其他:
打印(“请选择“e”或“d”)
if choice.lower()=“e”:
加密()
elif choice.lower()==“d”:
解密()
其他:
通过
打印(“”)
任务2选择()
再次定义()
再次=输入(“是否要重新启动?[y/n]:”)
再次返回。lower()==“y”
当main()或再次()时:
通过
出现此问题的原因是,当for循环检查空格并跳过关键字中的一个字母时。
任何帮助都将不胜感激。提前感谢。

您应该重新考虑使用
zip
-只需根据需要获取
next
键即可。不过,迭代器的使用很好@谢谢,但我用了拉链。还有其他想法吗?您可以在压缩字符串之前删除空格。@jonrsharpe输出中需要有空格。然后您必须停止使用
zip
。或者在键中插入空格的匹配字符,这将非常困难。