2016-11-12 27 views
0

我正在做某種密碼,需要一個字符串被倒讀,我也得到一個索引錯誤,即使我引用索引以及在範圍內:字符串索引不起作用? - Python的

M = str(input("Input Message: ")) 
M = M.upper() 
L = len(M) 
A = ["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"] 

def DECRYPT(): 
    global L 
    global M 
    global A 

    if L%2 != 0: 
     POS = False 
    else: 
     POS = True 

    i = L-1 
    NM = "" 
    while 1: 
     if M[i] != " ": 
      INDEX = A.index(M[i]) 
      if POS == True: 
       INDEX += (i + 1) 
      else: 
       INDEX -= (i) 
      INDEX %= 26   
      NM = NM+A[INDEX] 
      i += 1 
      if POS == True: 
       POS = False 
      else: 
       POS = True 
    print("\n"+NM) 

def ENCRYPT(): 
    global L 
    global A 
    global M 

    POS = True 
    M = M[::-1] 
    i = 0 
    NM = "" 

    while 1: 
     if i == L: 
      break 
     if M[i] != " ": 
      INDEX = A.index(M[i]) 
      if POS == True: 
       INDEX += (i + 1) 
      else: 
       INDEX -= (i + 1) 
      INDEX %= 26   
      NM = NM+A[INDEX] 
     i += 1 
     if POS == True: 
      POS = False 
     else: 
      POS = True 
    print("\n"+NM) 

while 1: 
    C = int(input("\nWhat do you want to do:\n1) Encrypt Something\n2)Decrypt Something\n\n")) 
    if C == 1: 
      ENCRYPT() 
    if C == 2: 
      DECRYPT() 

其中i是一個佔位符值。我運行它並得到這個:

Input Message: ABC 

What do you want to do: 
1) Encrypt Something 
2)Decrypt Something 

2 
Traceback (most recent call last): 
    File "C:\Users\Danny\Google Drive\SHIFT.py", line 67, in <module> 
    DECRYPT() 
    File "C:\Users\Danny\Google Drive\SHIFT.py", line 19, in DECRYPT 
    if M[i] != " ": 
IndexError: string index out of range 

我試過改變我的價值無濟於事。

+1

'CODE'休息導致錯誤 – ForceBru

+0

'String'是大概是空的 –

+2

錯誤發生在你沒有顯示的代碼部分。 – idjaw

回答

1

您的代碼有多個問題:正如fernand0指出的,您的索引運行方式錯誤;您的代碼反轉POS發生在不同的層次上,加密者在每個字符上都會這樣做,解密者在每個字母上都會這樣做 - 它們應該是一樣的;您的六個全球聲明中有五個是不需要的;你不正確地處理分詞,因此解密將不能匹配原始分詞;一旦您加密,在同一會話中無法解密,因爲本地NM未反饋到全球M

以下是我對代碼的返工,解決了上述問題和一些樣式問題。這裏的關鍵詞是'簡化'。我已經把你的奇怪的大寫變量名,但擴大了他們從單個字符到它們所代表:

MESSAGE = input("Input Message: ").upper() 
ALPHABET = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") 
ALPHABET_LENGTH = len(ALPHABET) 

def DECRYPT(MESSAGE): 

    LENGTH = len(MESSAGE) 
    IS_NEGATIVE = True 

    NEW_MESSAGE = "" 

    for I in range(LENGTH): 

     if MESSAGE[I] in ALPHABET: 
      INDEX = ALPHABET.index(MESSAGE[I]) 
      if IS_NEGATIVE: 
       INDEX -= (I + 1) 
      else: 
       INDEX += (I + 1) 
      INDEX %= ALPHABET_LENGTH 
      NEW_MESSAGE += ALPHABET[INDEX] 

      IS_NEGATIVE = not IS_NEGATIVE 
     else: 
      NEW_MESSAGE += MESSAGE[I] 

    return NEW_MESSAGE[::-1] 

def ENCRYPT(MESSAGE): 

    MESSAGE = MESSAGE[::-1] 

    LENGTH = len(MESSAGE) 
    IS_POSITIVE = True 

    NEW_MESSAGE = "" 

    for I in range(LENGTH): 

     if MESSAGE[I] in ALPHABET: 
      INDEX = ALPHABET.index(MESSAGE[I]) 
      if IS_POSITIVE: 
       INDEX += (I + 1) 
      else: 
       INDEX -= (I + 1) 
      INDEX %= ALPHABET_LENGTH 
      NEW_MESSAGE += ALPHABET[INDEX] 

      IS_POSITIVE = not IS_POSITIVE 
     else: 
      NEW_MESSAGE += MESSAGE[I] 

    return NEW_MESSAGE 

while True: 
    print("\nWhat do you want to do:") 
    print("1) Encrypt Message") 
    print("2) Decrypt Message") 

    CHOICE = int(input("\n")) 

    if CHOICE == 1: 
     MESSAGE = ENCRYPT(MESSAGE) 
    if CHOICE == 2: 
     MESSAGE = DECRYPT(MESSAGE) 

    print("\n" + MESSAGE) 

TEST

> python3 file.py 
Input Message: An opportunity to teach is an opportunity to learn 

What do you want to do: 
1) Encrypt Message 
2) Decrypt Message 

1 

OPDAQ HB OEWAGIBFXIU JD RI JZEZZ GC NFVBFJAGWJT KC 

What do you want to do: 
1) Encrypt Message 
2) Decrypt Message 

2 

AN OPPORTUNITY TO TEACH IS AN OPPORTUNITY TO LEARN 

What do you want to do: 
1) Encrypt Message 
2) Decrypt Message 
0

你開始我在字符串的末尾,然後,你正在增加它。我認爲行i + = 1應該是我 - = 1

+0

我已經更正了語法,但它對錯誤 – WhoGivesAFlux

+0

沒有影響好吧,還有更多問題。只有當你輸入if時,你纔會減少我,所以這是一個問題。而且,這個時候的條件是1,所以我會變成最終-1,這是超出範圍的。嘗試將打印(「我」,我,L-1) 下的while 1,你會看到發生了什麼。 – fernand0