2013-10-28 4 views
0

나는 비단 암호를 쓸데없는 암호로 작성하려고하는데, 도움이 필요하다. 함수의 코드가 끝날 무렵에는 도움이 필요합니다. 각 키를 시도한 후에 틈이 생길 수 있도록 구체적으로 인쇄하는 방법을 알고 싶습니다. 파이썬 3.3을 사용하고 있으며 3 주 전에 코딩을 시작했습니다. 시저 암호 무차별 암호 해독

print ("The plaintext will be stripped of any formatting such as spaces.") 
freqlist = [] 
maxkey = 26 
if key > maxkey: 
    raise Exception("Enter a key between 0 and 26: ") 
elif key < 0: 
    raise Exception("Enter a key between 0 and 26: ") 
freq = [] 
inpt=input("Please enter the cipher text:") 
inpt = inpt.upper() 
inpt = inpt.replace(" ", "") 
inpt = inpt.replace(",", "") 
inpt = inpt.replace(".", "") 
for i in range(0,27): 
     key = i 

def decrypt(): 
    for i in range(0,27): 
     for a in inpt: 
      b=ord(a) 
      b-= i 
      if b > ord("Z"): 
       b -= 26 
      elif b < ord("A"): 
       b+=26 
      freqlist.append(b) 
     for a in freqlist: 
      d=chr(a) 
      freq.append(d) 
      freq="".join(freq) 
      print(freq.lower(),"\n") 
decrypt() 

나는 루프를 사용하려고 그리고 난 정말 효과적으로 작동하고 있다고 생각하지 않습니다.

+2

죄송합니다 일부 변경 사항 설명과 함께, 당신의 decrypt 함수의 편집이다 어떤 오류 메시지) 아니면 그냥 효율적이지 않은가? 언어 문제 일지 모르지만 나는 그것을 이해하지 못했습니다. – Jblasco

+0

이제 모든 카이사르 암호에 대한 입력 빈도 테이블이 생겼습니다. 이 테이블을 가지고 무엇을 할 계획입니까? 어떤 암호가 맞는지 어떻게 결정할 건가요? –

+0

이 코드를 개선하는 방법에 대한 도움과 제안이 필요합니다. – iabestever

답변

1

귀하가 게시 한 오류에 따라, 이것이 도움이 될 것이라고 생각합니다.

파이썬에서는 같은 이름의 로컬 변수와 글로벌 변수를 가질 수 있습니다. 이 함수의 freq은 로컬이므로 로컬 freq의 초기화가 로컬의 초기화에 영향을주지 않습니다. 글로벌 freq을 사용하려면 global statement을 통해 해당 기능에 알리십시오. 이것은 Python FAQs에서 조금 더 설명됩니다.

궤도에서 돌아 오기에 충분해야합니다.

편집 : 아래 는 당신이, (만약 그렇다면 전혀 작동하지 않는 의미 "효율적으로 작업"에 의해,

def decrypt(): 

    # we don't need the zero start value, that's the default 
    # test all possible shifts 
    for i in range(27): 

     # initialize the array 
     freqlist = [] 

     # shift all the letters in the input 
     for a in inpt: 
      b = ord(a) 
      b -= i 
      if b > ord("Z"): 
       b -= 26 
      elif b < ord("A"): 
       b+=26 
      freqlist.append(b) 

     # now put the shifted letters back together 
     shifted = "" 
     for a in freqlist: 
      d = chr(a) 
      # append the shifted letter onto our output 
      shifted += d 

     # after we put the decrypted string back together, print it 
     # note this is outside the letter loops, 
     # but still inside the possible shifts loop 
     # thus printing all possible shifts for the given message 
     print(d) 
+0

감사합니다. 이것은 나를 도왔지만 나는 또한 내 결과를 발표하는 데 도움이 될 더 많은 것을 찾고있었습니다. – iabestever