2017-03-17 27 views
0

好了,所以我寫了下面的一系列功能在Python 3.6.0:python中的加密代碼,調用函數,python不返回任何內容。沒有錯誤消息顯示

def code_char(c, key): 
    result = ord(c) + key 
    if c.isupper(): 
    while result > ord("Z"): 
     result -= 26 
    while result < ord("A"): 
     result += 26 
    return chr(result) 
    else: 
    while result > ord("z"): 
     result -= 26 
    while result < ord("a"): 
     result += 26 
    result = chr(result) 
    return result 

def isletter(char): 
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122: 
      return True 
    else: 
      return False 

def encrypt(string, key): 
    result = "" 
    length = len(string) 
    key = key * (length // len(key)) + key[0:(length % len(key))] 
    for i in range(0,length): 
      if (isletter for i in string): 
        c = string[i] 
        num = int("".join("".join(i) for i in key)) 
        result += code_char(c, num) 
      else: 
        c = string[i] 
        result += i 
    return result 

然後我嘗試在調用的函數:

encrypt("This is a secret message!!", "12345678") 

當蟒蛇運行程序絕對沒有任何反應。沒有東西會被返回,並且在shell中python強制我進入沒有縮進的空行,或>>>。我不知道什麼是對錯的,因爲沒有錯誤信息出現,並且沒有結果出現。任何類型的建議,將不勝感激。

謝謝。

+0

爲什麼你認爲你的函數'isletter'比標準的'isalpha'好? – DyZ

+0

回到你的問題:你的一個'while'循環,可能不止一個,是無限的。 – DyZ

+0

@DYZ我避免使用標準的'isalpha',因爲這是給定賦值的要求。感謝您提供'while'循環的建議。我現在試試看看它是否能解決我的問題。 – TwoFourSixOhOne

回答

0

在你的代碼看,我不認爲這是一個無限循環。我認爲你的循環不會是無限的,但會運行很長時間,因爲key的值非常大,因此,一次減去26,直到獲得英文字母ascii值,將會永遠持續(但不是真的永遠)

>>> key = '12345678' 
>>> length = len("This is a secret message!!") 
>>> key * (length // len(key)) + key[0:(length % len(key))] 
'12345678123456781234567812' 

這可能是在你的邏輯出了問題,也許在邏輯生成密鑰,但如果這確實是你想要的邏輯,如何使用模量,而不是迭代:

def code_char(c, key): 
    result = ord(c) + key 
    if c.isupper(): 
     if result > ord("Z"): 
      result = ord("Z") + result % 26 - 26 
     if result < ord("A"): 
      result = ord("A") - result % 26 + 26 
     return chr(result) 
    else: 
     if result > ord("z"): 
      result = ord("z") + result % 26 - 26 
     if result < ord("a"): 
      result = ord("a") - result % 26 + 26 

    return chr(result) 

def isletter(char): 
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122: 
      return True 
    else: 
      return False 

def encrypt(string, key): 
    result = "" 
    length = len(string) 
    key = key * (length // len(key)) + key[0:(length % len(key))] 
    for i in range(0,length): 
      if (isletter for i in string): 
        c = string[i] 
        num = int("".join("".join(i) for i in key)) 
        result += code_char(c, num) 
      else: 
        c = string[i] 
        result += i 
    return result 


>>> encrypt("This is a secret message!!", "12345678") 
'Rlmwrmwrerwigvixrqiwwekiss' 
+0

啊,我明白了。我試圖讓該鍵的每個索引都是該字符串各自索引的shift鍵,而不是每個索引都被整個鍵移位。那我一定在做別的事情。儘管謝謝你的建議! – TwoFourSixOhOne

+0

@TwoFourSixOhOne,請參閱我上面修改的回覆和評論。我相信你的代碼的關鍵是太大而不能僅僅通過減少來解決。你可能想看看其他途徑,可能使用一些線性代數或密碼/解密模塊。 –

0

如果你在這裏有while循環,或者你正在編制if循環嗎?我沒有看到while循環的任何退出。這可能是你的代碼掛起的地方。

if c.isupper(): 
    while result > ord("Z"): 
     result -= 26 
    while result < ord("A"): 
     result += 26 
    return chr(result) 
    else: 
    while result > ord("z"): 
     result -= 26 
    while result < ord("a"): 
     result += 26 

另外,如果我取代whileif以上,它給我的溢出錯誤。

OverflowError: Python int too large to convert to C long 

編輯 看着@馬球的評論,並採取在第二碼看後,我相信@polo是正確的。我把while環回,並添加print報表。我評論過他們,但你可以在你的最後取消註釋。
我也將密鑰的複雜性降低到key = key,並將密鑰從12345678降低到只有1234,以查看代碼是否正常工作以及是否在合理時間內完成。一旦代碼順利運行,您可以使其儘可能複雜。

下面是結果我得到了後:

>>> 
key =1234 
coding char = T 
coding char = h 
coding char = i 
coding char = s 
coding char = 
coding char = i 
coding char = s 
coding char = 
coding char = a 
coding char = 
coding char = s 
coding char = e 
coding char = c 
coding char = r 
coding char = e 
coding char = t 
coding char = 
coding char = m 
coding char = e 
coding char = s 
coding char = s 
coding char = a 
coding char = g 
coding char = e 
coding char = ! 
coding char = ! 
encrypted_message = Ftuezuezmzeqodqfzyqeemsqaa 

修改下面的代碼:

def code_char(c, key): 
    result = ord(c) + key 
    if c.isupper(): 
    while result > ord("Z"): 
     #print("result1 = {}",format(result)) 
     result -= 26 
    while result < ord("A"): 
     #print("result2 = {}",format(result)) 
     result += 26 
    return chr(result) 
    else: 
    while result > ord("z"): 
     #print("result3 = {}",format(result)) 
     result -= 26 
    while result < ord("a"): 
     #print("result4 = {}",format(result)) 
     result += 26 
    result = chr(result) 
    return result 

def isletter(char): 
    if 65 <= ord(char) <= 90 or 97<= ord(char) <= 122: 
      return True 
    else: 
      return False 

def encrypt(string, key): 
    result = "" 
    length = len(string) 
    #key = key * (length // len(key)) + key[0:(length % len(key))]  
    key = key 
    print "key ={}".format(key) 
    for i in range(0,length): 
      if (isletter for i in string): 
        c = string[i] 
        num = int("".join("".join(i) for i in key)) 
        print("coding char = {}".format(c)) 
        result += code_char(c, num) 
      else: 
        c = string[i] 
        result += i 
    return result 

#encrypt("This is a secret message!!", "12345678") 
encrypted_message = encrypt("This is a secret message!!", "1234") 
print("encrypted_message = {}".format(encrypted_message)) 
+0

@ Anil_M我現在用'if'取代了'while',現在我收到了同樣的錯誤信息!感謝您的建議。 – TwoFourSixOhOne

+0

@Anil_M如果你用if替換,你會嘗試將結果轉換爲聊天,當它仍然非常大,比chr()的範圍限制大得多。如果您在調用chr函數之前打印結果,則可以看到您嘗試轉換的數字並在解釋器中測試自己。我不確定它爲什麼會引發溢出錯誤而不是ValueError – polo

+0

@TwoFourSixOhOne,您的代碼和邏輯很好。主要的問題是你輸入字符串的長度合併後做得太大而又做得更大。我已經簡化了關鍵字並將打印語句放在代碼中。它確實按預期工作。您可能想要更有效地解析密鑰。大概在SO上發表另外一個問題就是如何做到這一點。 –

相關問題