2016-07-28 35 views
0

當我試圖運行這個它說NameError:名字「加密」沒有定義。Python的NameError:名字「加密」沒有定義

MAX_KEY_SIZE = 26 
def getMode(): 
    while True: 
      print('Do you wish to encrypt or decrypt a message?') 
      mode = input().lower() 
      if mode in "encrypt" 'e' 'decrypt' 'd'.split(): 
       return mode 
      else: 
       print('Enter either "encrypt" or "e" or "decrypt" or "d".') 
+1

你使用Python 2還是Python 3?'print'函數看起來像3,但是你得到的'NameError'會來自2的'input'版本。 (請參閱[progo的答案](https://stackoverflow.com/questions/38631474/38631721#38631721)爲什麼它很重要。) –

+0

看起來,這是Python 2代碼,帶'from __future__ import print_function' isn' t顯示在這裏。 –

回答

2

從我所瞭解的代碼中,'encrypt'是一個字符串值。您需要創建一個包含所需字符串值的列表,並檢查模式變量是否與該列表中的值匹配。

MAX_KEY_SIZE=26 
def getMode(): 
    while True: 
     mode=input().lower() 
     if mode in ['encrypt','e','decrypt','d']: 
      return mode 
     else: 
      print('Enter either "encrypt" or "e" or "decrypt" or "d".') 

如果你想使用.split()方法,你可以做到以下幾點:

if mode in "encrypt e decrypt d".split() 
+0

或''將e解密d「.split()'加密,使列表就位。 –

+0

此外,可以執行'ALLOWED_ANSWERS = ['encrypt','e','decrypt','d']',然後在ALLOWED_ANSWERS:和之後輸入'print'(「輸入」+「或」。加入(ALLOWED_ANSWERS)+「。」)'或甚至'print(「在ALLOWED_ANSWERS中輸入」+「或」.join(''''+ i +'「')+」。「)' – glglgl

+1

雖然這解決了_a_問題,但它並沒有解決Nic Pismiris遇到的'NameError'。請參閱[pogo的回答](https://stackoverflow.com/questions/38631474/38631721#38631721)。 (對於它的價值,我也是爲此而犧牲的。) –

1

疑難雜症! input試圖評估您的輸入(因此,它的名字非常誤導)。使用raw_input以字符串格式捕獲用戶的意願。

基本上什麼input確實是需要raw_input和管道它eval:現在你要評估一個字符串「加密」爲Python代碼,因此它具有寫作同樣的效果「加密」到您的文件。自然會導致錯誤,因爲沒有這樣的變量被引入任何地方。 evalinput都是非常危險的東西,所以儘量不要使用它們,對於它們來說很少有真正的用例。圍繞這個網站這種差異

更多信息: https://stackoverflow.com/a/15129556/308668

+0

好的。我在括號中看到了'print'函數,並假定代碼是Python 3(就像其他人一樣,似乎......)。 –

0
MAX_KEY_SIZE = 26 
def getMode(): 
    while True: 
     print ('Do you wish to encrypt or decrypt a message?') 
     mode = input().lower() 
     if mode in "encrypt" 'e' 'decrypt' 'd'.split(): 
      return mode 
     else: 
      print('Enter either "encrypt" or "e" or "decrypt" or "d".') 

希望這是你的代碼..如果是的,那麼它不應該給任何錯誤,也該方法,你正試圖獲得結果據推測不會解決你的目的,因爲"encrypt" 'e' 'decrypt' 'd'.split()會給你['encryptedecryptd'],你無法通過「入法」,你正在嘗試的搜索模式。在列表if any(mode in s for s in "encrypt" 'e' 'decrypt' 'd'.split()): or you can store「加密」「E」'解密「德',然後使用‘入法’,以配合用戶的輸入:要麼你可以搜索喜歡的模式。

希望它可以幫助..

+0

這並不能解決Python 2的'input'產生的'NameError'。 (Nic Pismiris沒有指定他使用的是哪個版本的Python,但是Python 3的'input'不會產生這個錯誤。) –

+0

另外,'如果有的話(s中的模式在split ....中) '將遍歷同一個字符串列表,使其與'encryptedecryptd'中的if模式實際上相同:'。所以,如果'mode'是''encrypt'',它會返回'True',而且如果它是'ted''或''cry''或''edecr''則返回'True'。您的最後一個選項---允許的字符串列表---更加精確。 –

+0

感謝Kevin對於**「any」**失敗的用例,同時尋找代碼,確定代碼是用** python 3 **編寫的,同樣使用input()將給出**「NameError」 **如** python 3 **的情況下**如果用戶需要輸入字符串或字符,用戶需要將輸入放入**雙引號/單引號**中,所以在上述情況下,輸入應該可以:**「加密」**而不是**加密**。 – Sumit

0

擴展在pogo's answer,這是正確的...

讓我吃驚的(顯然許多其他的)是串在if mode in ...:線羣集一個語法錯誤。

if mode in "encrypt" 'e' 'decrypt' 'd'.split(): 

這些字符串都是編譯時間常數,所以string literal concatenation膠水他們進入一個字符串開始執行前:

>>> "encrypt" 'e' 'decrypt' 'd' 
'encryptedecryptd' 

split()方法,然後調用該字符串,這是偶然呢不包含任何空格。返回值是包含一個字符串列表:

>>> "encrypt" 'e' 'decrypt' 'd'.split() 
['encryptedecryptd'] 

in運營商不會再抱怨被賦予一個字符串(mode)和字符串列表,但它會返回False以外的mode每個值一個......沒有人可能會輸入:

>>> 'encrypt' in ['encryptedecryptd'] 
False 
>>> 'encryptedecryptd' in ['encryptedecryptd'] 
True 
相關問題