2016-02-28 119 views
1

我一直在玩Python和對面麻省理工學院,這一任務是創建編碼消息(凱撒代碼中,例如,你的消息改變ABCD字母CDEF)來了。這是我想出了:選擇從列表中選擇特定int類型,並改變它們

Phrase = input('Type message to encrypt: ') 
shiftValue = int(input('Enter shift value: ')) 

listPhrase = list(Phrase) 
listLenght = len(listPhrase) 

ascii = [] 
for ch in listPhrase: 
    ascii.append(ord(ch)) 
print (ascii) 

asciiCoded = [] 
for i in ascii: 
    asciiCoded.append(i+shiftValue) 
print (asciiCoded) 

phraseCoded = [] 
for i in asciiCoded: 
    phraseCoded.append(chr(i)) 
print (phraseCoded) 

stringCoded = ''.join(phraseCoded) 
print (stringCoded) 

代碼工作,但我要實現不轉移的空間和消息特殊標誌的ASCII值。

所以我的想法是在範圍(65,90)和量程(97122)範圍內選擇列表值和改變他們,而我不會改變任何人。但我該怎麼做?

+0

你可以使用'isalpha()'方法http://stackoverflow.com/questions/15558392/how-to-check-if-character-in-string-is-a-word-character-python – RinaLiu

回答

0

如果你想使用巨大的代碼:)做這麼簡單的東西,那麼你把一張支票,像這樣:

asciiCoded = [] 
for i in ascii: 
    if 65 <= i <= 90 or 97 <= i <= 122: # only letters get changed 
     asciiCoded.append(i+shiftValue) 
    else: 
     asciiCoded.append(i) 

但是你知道嗎,Python可以做全在一行中,使用list comprehension。關注此:

Phrase = input('Type message to encrypt: ') 
shiftValue = int(input('Enter shift value: ')) 

# encoding to cypher, in single line 
stringCoded = ''.join(chr(ord(c)+shiftValue) if c.isalpha() else c for c in Phrase) 

print(stringCoded) 

一點解釋:列表理解歸結爲這個for循環,這是容易理解。抓到了什麼? :)

temp_list = [] 
for c in Phrase: 
    if c.isalpha(): 
     # shift if the c is alphabet 
     temp_list.append(chr(ord(c)+shiftValue)) 
    else: 
     # no shift if c is no alphabet 
     temp_list.append(c) 

# join the list to form a string 
stringCoded = ''.join(temp_list) 
+0

如果你不希望有 「幻數」 左右浮動,'65','90','97'和'122'是'ORD( 'A')','ORD( 'Z')','ORD ('a')'和'ord('z')'。 – pzp

+0

我知道,但是從OP看來,推測OP也知道它似乎是合理的。所以使用這些神奇的數字沒有什麼壞處保重,兩個OP意味着不同。 :) –

+0

Shadowfax你能告訴我,還是將我連接到解釋,那些雙括號是做什麼的?對於這裏egsample:stringCoded = ''。加入(temp_list) – RadekE

-1
asciiCoded = [] 
final_ascii = "" 
for i in ascii: 
    final_ascii = i+shiftValue #add shiftValue to ascii value of character 
    if final_ascii in range(65,91) or final_ascii in range(97,123): #Condition to skip the special characters 
     asciiCoded.append(final_ascii) 
    else: 
     asciiCoded.append(i) 
print (asciiCoded) 
+0

對於每種情況,都會丟失'Z'和'z'。去搞清楚。 –

+1

'range()'排除端點。你想'範圍(65,91)'和'範圍(97,123)'。 – pzp

+0

它只是因爲如果u試圖爲Z/Z添加位移值即其90 +任何移位或122+任何偏移值將它帶到其他非alphacharacters> 91或122 – user5993403

0

容易得多它是從string模塊使用maketrans方法:

>>import string 
>> 
>>caesar = string.maketrans('ABCD', 'CDEF') 
>> 
>>s = 'CAD BA' 
>> 
>>print s 
>>print s.translate(caesar) 
CAD BA 
ECF DC 

編輯:這是爲Python 2.7

3.5只是做

caesar = str.maketrans('ABCD', 'CDEF') 

和易於函數返回一個映射。

>>> def encrypt(shift): 
...  alphabet = string.ascii_uppercase 
...  move = (len(alphabet) + shift) % len(alphabet) 
...  map_to = alphabet[move:] + alphabet[:move] 
...  return str.maketrans(alphabet, map_to) 
>>> "ABC".translate(encrypt(4)) 
'EFG' 

該函數使用模加法構造加密的凱撒字符串。

+0

的OP標籤爲python 3.x.你的是Python 2.x.並沒有考慮到用戶輸入的shiftValue。如果要處理A-Z和a-z,那將非常笨拙。 –

+0

謝謝Shadowfax。顯然我是一個懶惰的讀者;-)我已更新我的評論以反映您的評論。 – jazz