2015-12-12 31 views
-1

我在尝试解决代码中的错误时遇到问题。它不打印最终产品并留下空白区域。如何强制我的代码在python中打印

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 

再说你是问是否结束时仍处于加密的循环。

但是,您的设计中还存在其他缺陷。如果键长度超过1个字符,则由于keyList只有1个项目而导致索引错误。虽然您期望for循环中的keyList中有更多项目。

+0

我该如何解决这个问题?顺便说一句,感谢这有助于:) – xxowainxx

+0

什么你想解决什么时候有帮助?标签间距? – Radek

+0

它帮助我更好地理解它,但我无法获得标签间距正确 – xxowainxx