2014-12-08 1 views
0

입력 된 문자열을 ASCII 값으로 바꾸고 3을 추가 한 다음 모든 문자가 변경된 새 문자열을 출력해야합니다.파이썬 문자열을 ASCII로 다시 입력

내가 목록에서 ASCII 값을 얻을 수있는 지점에 도달했지만 문자열로 다시 변환하는 데 문제가 있습니다.

이것은 내가 지금까지 작성한 코드입니다.

def encrypt (x): #function for encrypting 
#y=[ord(c) for c in x] #turns strings into values this works 
#y=[3+ ord(c) for c in x] #adds 3 to the values of the letters turned to number this also works 
y=str([3+ ord(c) for c in x]) # this does not do what I expected it to do. Neither does char 
print(y) 

'''def decrypt (x): 
y=str([-3 + ord(c) for c in x]) 
print(y) 
''' 


x=str(input("Enter something to be encrypted: ")) #gets string to encrypted 

encrypt (x) #calls function sends the entered sentence 

''' 
x=str(input("Enter something to be decrypted: ")) #gets string to decrypted 

decrypt (x) 

''' 

는 내가 편지, 나는 아마 나머지를 알아낼 수 변경으로 문자열을 돌려줘 얻을 수 있다면, 다시 돌려의 두 번째 부분에서 댓글을 달았습니다.

도움을 주셔서 감사합니다.

답변

3

str은 발견 한대로 정수로 표시되며 ABC이 아닌 [65, 66, 67]과 같은 문자열을 제공합니다. 단일 정수를 다시 문자열로 변환하려면 chr을 사용할 수 있습니다. 그런 다음 전체 정수 목록에서 사용하려면 join을 사용하십시오.

y = ''.join(chr(3 + ord(c)) for c in x) 
+0

감사합니다. –

관련 문제