2012-11-03 3 views
3

내 친구 중 한 명과 재미있게 지내기 위해 노력하고 있습니다. 특수 요원이지만 특별한 비밀 메시지 코드가 없어도 어떻게 통신 할 수 있습니까? 고려문자열을 암호화하여 문자열을 암호화하는 방법

fgtwi rnxxnts, itsy fyp yjfhmjw! 

:

# txt = the secret message to convert! 
# n = number of times to jump from original position of letter in ASCII code 

def spy_code(txt,n): 
    result = '' 
    for i in txt: 
     c = ord(i)+n 
     if ord(i) != c: 
      b = chr(c) 
      result += b 
    print result 

spy_code('abord mission, dont atk teacher!',5) 

비밀 메시지로 메시지를 변환 한 후 우리는 한 줄의 텍스트를 얻고있다 ...

fgtwi%rnxxnts1%itsy%fyp%yjfhmjw& 

문제는 우리가 이러한 결과를 달성하고 싶은 것이있다 편지 만.

오직 스파이 코드를 사용하여 글자를 변환하고 공백이나 특수 기호를 변환하지 마십시오.

+0

를, 그것은 본다 문자가 문자 인 경우 변환합니다. 'string' 모듈에는 대문자와 소문자 문자열이 있습니다. 'import string; string.letters' – GP89

+1

나는 부정 투표를 정확히 얻지 못합니다 : s. 이것은 나를 위해 올바른 질문이다. – jlengrand

+0

@hayden, 미안 해요,'a'는'c'입니다. 바로 수정되었습니다. Jlengrand 그들은 양과 같아서 beeep을 허용합니다. – user1768615

답변

2

간단한 방법으로 GP89 팁을 사용하십시오.

현재 문자가 문자 목록에 있는지 단순히 확인하십시오. 그렇지 않으면 그냥

import string 

vals = string.letters 
def spy_code(txt,n): 
    result = '' 
    for i in txt: 
     if i in vals: 
      c = ord(i)+n 
      if ord(i) != c: 
       b = chr(c) 
       result += b 
     else: 
      result += i 
    print result 

spy_code('abord mission, dont atk teacher!',5) 

반환

fgtwi rnxxnts, itsy fyp yjfhmjw! 
0

내가 지금은 오래로 반환하지만 str.isalpha() 사용할 수 있습니다 만 원하는 같은 단서로

s = 'Impending doom approaches!' 
n = 6 
result = '' 

for c in s: 
    result += chr(ord(c) + n) if c.isalpha() else c 

print(result) 
>>> Osvktjotm juus gvvxuginky! 
관련 문제