好了,所以我寫了下面的一系列功能在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強制我進入沒有縮進的空行,或>>>。我不知道什麼是對錯的,因爲沒有錯誤信息出現,並且沒有結果出現。任何類型的建議,將不勝感激。
謝謝。
爲什麼你認爲你的函數'isletter'比標準的'isalpha'好? – DyZ
回到你的問題:你的一個'while'循環,可能不止一個,是無限的。 – DyZ
@DYZ我避免使用標準的'isalpha',因爲這是給定賦值的要求。感謝您提供'while'循環的建議。我現在試試看看它是否能解決我的問題。 – TwoFourSixOhOne