0

我有一点 Caesar Cipher 代码,但无法尝试 /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

我已经尝试过不同的位置,除了尝试之外......我迷路了。提前致谢!

4

3 回答 3

0

在我在这里修复了不匹配的标识之后,我已经运行了你的代码,看起来没问题:

      if char in validLetters:
      newString += char

对此:

      if char in validLetters:
          newString += char

我得到以下输出:

Please enter a sentence : test
Please enter your shift : 

INCORRECT INPUT!
于 2013-11-06T18:39:43.227 回答
0

这是原始问题的OT,但是

result = raw_input("Enter Q, D, or E")
while result.lower() not in ["q","d","e"]:
      result = raw_input("Invalid Response! Enter Q,D, or E:")
#now you know result is one of q d or e

可能是获得您输入的更好方法

于 2013-11-06T19:31:12.960 回答
0

try..except如果您或系统引发异常,则使用块。except如果块成功完成,则不会执行该块try。在您的情况下,您的程序根本不会进入if块。

为了得到预期的结果,重写 if 语句如下:

    if encrypt == 'e':
        ...
    elif encrypt == 'd':
        ...
    elif encrypt == 'q':
        break
    else:
        print("")
        print("INCORRECT INPUT!")
        print("")

这样,您可以结合所有条件,并拥有一个“默认”语句。如果没有输入前面的 s AND 第一个,则else后面的 s 将输入(这基本上是说没有任何条件为真)。elifelifif

如果您不使用elif并且仅使用if(如在您的原始代码中),则条件链被破坏并且else最后的 将捕获所有情况encrypt != "q",即使它是“e”或“d”。

于 2013-11-06T18:42:57.267 回答