我如何强制我的代码在 python 中打印

Posted

技术标签:

【中文标题】我如何强制我的代码在 python 中打印【英文标题】:How do i force my code to print in python 【发布时间】:2015-12-12 23:16:18 【问题描述】:

我在尝试解决我的代码中的错误时遇到了麻烦。它不会打印最终产品并留下空白。

playing = True
string = ""
Alphabet = ('z','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')

while playing == True:
    string = ""
    eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')

    if eord == 'e':
        texte = input ("Type your word to encrypt: ")
        key1 = int(input("Choose a key between 1-26: "))
        for letter in texte:
            number = (ord(letter)) + (key1)
            letter=(chr(number))
            string = (str(string)) + (str(letter))
        print (string)
        keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

        if keyword == 'encrypt':
            plainText = input("Please enter the plain text: ")
            key = input("Please enter the key: ")
            keyList = []
            keyLength = 0
            while keyLength < len(plainText):
                for char in key:
                     if keyLength < len(plainText):
                         keyList.append(str(char))
                         keyLength = keyLength + 1
                         CipherText = [] 
                         IndexValue = 0
                         keyIncrement = 0
                     for plainTextChar in plainText:
                         IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
                         while IndexValue > 26:
                             IndexValue = IndexValue - 26
                             CipherText.append(Alphabet[IndexValue])
                             keyIncrement = keyIncrement + 1
                         print (''.join(CipherText))
                         import sys
                         sys.stdout.flush()

                         finish = input('Would you like to go again Y or N')
                         if finish == 'n' or 'N':
                             retry = input ("Would you like to go again? Y or N: ")
                             if retry == 'N' or 'n':
                                 print ("Please exit the window")
                                 import sys
                                 sys.exit()

    elif eord == 'd':
        texd = input ("Type your word to decrypt: ")
        key2 = int(input("Choose a key between 1-16: "))

        for letter in texd:
            number = (ord(letter)) - (key2)
            letter=(chr(number))
            string = (str(string)) + (str(letter))
        print (string)
        keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")

        if keyword == 'decrypt':
             plainText = input("Please enter the plain text: ")
             key = input("Please enter the key: ")
             keyList = []
             keyLength = 0
             while keyLength < len(plainText):
                 for char in key:
                     if keyLength < len(plainText):
                         keyList.append(str(char))
                         keyLength = keyLength - 1
                         completeCipherText = [] 
                         cipherCharIndexValue = 0
                         keyIncrement = 0
                     for plainTextChar in plainText:
                         cipherCharIndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
                         while cipherCharIndexValue > 26:
                             cipherCharIndexValue = cipherCharIndexValue + 26
                             completeCipherText.append(Alphabet[cipherCharIndexValue])
                             keyIncrement = keyIncrement - 1
                         print (''.join(completeCipherText))

                         finish = input('Would you like to go again Y or N')
                         if finish == 'n' or 'N':
                             retry = input ("Would you like to go again? Y or N: ")
                             if retry == 'N' or 'n':
                                 print ("Please exit the window")
                                 import sys
                                 sys.exit()

有什么办法可以解决这个问题或强制打印吗?

这是代码的输出。

Type "d" to "decrypt" and "e" to "encrypt": e
Type your word to encrypt: hello
Choose a key between 1-26: 3
khoor
Type 'encrypt' code further or 'decrypt' further: encrypt
Please enter the plain text: python
Please enter the key: cipher

Would you like to go again Y or N

【问题讨论】:

【参考方案1】:

标签间距有问题。具体来说:

                     while IndexValue > 26:
                         IndexValue = IndexValue - 26
                         CipherText.append(Alphabet[IndexValue])
                         keyIncrement = keyIncrement + 1

对于第一次传递,IndexValue 是 19,因此它也跳过了 CipherText 附加。因此没有任何东西可以打印出来。

此处的缩进正确:

                     while IndexValue > 26:
                         IndexValue = IndexValue - 26
                     CipherText.append(Alphabet[IndexValue])
                     keyIncrement = keyIncrement + 1

然后你又在询问是否在加密 for 循环中完成。

但是,您的设计中还有其他缺陷。如果键长于 1 个字符,则由于 keyList 只有 1 个项目,您最终会出现索引错误。虽然您期望 for 循环中的 keyList 中有更多项目。

【讨论】:

如果有帮助,您想解决什么问题?制表符间距? 它帮助我更好地理解它,但我无法正确设置制表符间距 我假设您希望将 IndexValue 减少到您的 Alphabet 地图的范围内。所以其余的 while 行在那里不相关,应该向左隔开。 哦,好的,我现在看到了@Radek +1 指出的问题。我正在查看此脚本中的其他问题。 谢谢你解决了这个问题。但是它现在显示列表索引超出范围。

以上是关于我如何强制我的代码在 python 中打印的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Python 代码在从文本文件中读取时会打印额外的字符“”?

强制黑白打印

如何解决 Python 中的 StopIteration 错误?

强制打印功能中的行尾与python-shell Node.js中的原始字符串相同

如何在我的视图中打印 JavaScript 代码?

为啥我的函数在 python 中没有打印 [重复]