Caesar Cipher コードが少しあり、try/except が機能しません。最初の入力まで繰り返すだけです。「INCORRECT INPUT」は表示されません。これを修正するにはどうすればよいですか?
while True:
try:
encrypt = raw_input("Would you like to encrypt or decrypt a message? (E/D) : ").lower()
print("")
if encrypt == 'e':
print("ENCRYPTION: Due to the nature of the Caesar Cipher, Numbers and Symbols will
be removed. Please represent numbers as the word...")
print("I.E. - 7 should be entered as 'seven'. ")
print("")
sentence = raw_input("Please enter a sentence : ").lower()
newString = ''
validLetters = "abcdefghijklmnopqrstuvwxyz "
space = []
for char in sentence:
if char in validLetters or char in space:
newString += char
shift = input("Please enter your shift : ")
resulta = []
for ch in newString:
x = ord(ch)
x = x+shift
resulta.append(chr(x if 97 <= x <= 122 else 96+x%122) if ch != ' ' else ch)
print sentence
print("")
print("Your encryption is :")
print("")
print ''.join(resulta)
if encrypt == 'd':
print("DECRYPTION : PUNCTUATION WILL NOT BE DECRYPTED")
print("")
sentence = raw_input("Please enter an encrypted message : ").lower()
newString = ''
validLetters = "abcdefghijklmnopqrstuvwxyz "
for char in sentence:
if char in validLetters:
newString += char
shift = input("Please enter your shift : ")
decryptshift = 26 - shift
resulta = []
for ch in newString:
x = ord(ch)
x = x + decryptshift
resulta.append(chr(x if 97 <= x <= 122 else 96+x%122) if ch != ' ' else ch)
print sentence
print("")
print("Your encryption is :")
print("")
print ''.join(resulta)
if encrypt == 'q':
break
except:
print("")
print("INCORRECT INPUT!")
print("")
continue
私は試行と例外の両方でさまざまな場所を試しました...私は迷っています。前もって感謝します!