2013-05-05 42 views
0

之前我沿着每個選項選擇內的打印菜單重複類似的代碼,但我改變它,如果和ELIF但不工作?它不斷想出語法錯誤不斷爲我的elif語句提出無效的語法錯誤

option = 0 

while option !=4: 

    # Prints the menu to the screen 
     print("*** Menu *** \n") 
     print("1. Encrypt string") 
     print("2. Decrypt string") 
     print("3. Brute force decryption") 
     print("4. Quit \n") 

# Prompts the user 
option = int(input("What would you like to do [1,2,3,4]? ")) 

# input error 

if option ==1: 
#if the user's selection equals 1 


      # Prints to the screen the chosen option and prompts the user to input 
      print("In command 1 - encrypt string \n") 
      string = input("Please enter string to encrypt: ") 
      offsetvalue = int(input("Please enter positive offset value: ")) 
      print("") 

# input error 
while offsetvalue > 26 or offsetvalue < 1: 
    offsetvalue = int(input("Please enter a positive offset value between 1 - 26: ")) 

    # Assigns the 'Encryption' variable 
    # letter which is encrypted 

    Encryption = "" 

# Loops the input in variable 'code' for each letter 
for letter in string: 
    # Converts each letter in 'code' to an ASCII value 
    # and adds the positive offset value 
    encryption_num = ord(letter) 
    encryption_num += offsetvalue 

# Loops the ASCII value 
if encryption_num > 126: 
     encryption_num -= 94 
     # adds it to the total string 
     encryption_num = chr(encryption_num) 
     Encryption += encryption_num 
     # encrypted string to the screen 
     print("\nEncrypted string:") 
     print(Encryption) 

elif option == 2: 

     # prompts the user to input 
     print("In command 2 - decrypt string \n") 
     string = input("Please enter string to decrypt: ") 
     offsetvalue = int(input("Please enter negative offset value: ")) 
     print("") 

# input error 

while offsetvalue < -26 or offsetvalue > -1: 
    offsetvalue = int(input("Please enter negative offset value between -1 - -26: ")) 

# Assigns the 'Decryption' variable 
# letter which is decrypted 
    Decryption = "" 

# Loops the input in variable 'string' 

for letter in string: 
    # adds variable 'string' to an ASCII value 
    # adds negative offset value 
    decryption_num = ord(letter) 
    decryption_num += offsetvalue 
    # Loops the ASCII value to beginning if True 
    if decryption_num < 32: 
      decryption_num += 94 
      # Converts the letter back into a string and adds it to the total string 
      decryption_num = chr(decryption_num) 
      Decryption += decryption_num 
      # Prints the entire decrypted string to the screen 
      print("\nDecrypted string:") 
      print(Decryption) 

這是它突出了誤差與elif的語句不知道爲什麼?它會在python中出現無效的消息。

elif option == 3: 
    print("in command 3 -brute force \n") 
    string = input("please enter string to decrypt: ") 
    offsetvalue = -0 
    while offsetvalue != -26: 
    decryption_num = 0 
    Decryption = "" 
while Decryption < len(letter): 
     c = ord(string[decryption_num]) + offsetvalue 
     if c < 0: 
     c += 128 
     decryption_num = chr(c) 
     Decryption += decryption_str 
     decryption_num += 1 
     offsetvalue -= 1 
     print ("\noffset", offsetvalue, "=Decrypted string:", Decryption) 

    # Prompts the user for their selection 
    option = int(input("What would you like to do [1,2,3,4]? ")) 

#input error 
elif option == 4: 
     print ("\nGoodbye") 
+0

請顯示完整的錯誤信息。 – BrenBarn 2013-05-05 17:28:57

回答

1

你不能有一個whileforelifelif只能在if或另一個elif後出現。你的意思是while在之前的if下縮進了嗎?

您的代碼有其他問題。你似乎在比較一個字符串與一個數字(Decryption < len(letter))。

你有這樣的事情:

if option == 2: 
    # stuff 

while something: 
    # stuff 

elif option == 3: 
    # stuff 

你不能做到這一點。您的意思是不是這樣的:

if option == 2: 
    # stuff 

    while something: 
     # stuff 

elif option == 3: 
    # stuff 

然而,我不能告訴你什麼改變,因爲我不知道你希望你的代碼做正是。我建議你閱讀the Python tutorial以熟悉Python的基礎知識。

+0

'elif'出現在'for'之後,但是,這是問題,*粗略*。 – 2013-05-05 17:30:56

+0

@MartijnPieters:如果他的意思是他的第二個代碼片段在第一個代碼之後立即執行,那麼這是真的。但最後他在'while'之後有'elif'。 – BrenBarn 2013-05-05 17:32:04

+0

的確,那是另一個會出現同樣錯誤的地方。 – 2013-05-05 17:32:48