python cesar密码问题

时间:2019-02-23 23:16:29

标签: python syntax-error spyder

我是编码的新手,一直在尝试制作Cesar密码(通过观看youtube视频)很幸运,但是一直遇到语法错误。有人可以帮忙吗?

代码如下:

result = ""
message = ""
choice = ""

while choice != 0:
    choice = input ("\ndo you want to encrypt or decrypt a message?\nEnter1 to encrypt, 2 to decrypt and 0 to exit. ")

    if choice == '1':
        message = input ("\nEnter message for encrpytion: ")
        for i in range(0, len(message)):
            result = result +chr(ord(message)[i] - 2


                                 print (result + ('\n\n' )
                                 result = ''

                             elif choice == '2':
                                 message = input ("\nEnter message to be decrypted: ")
                                 for i in range (0, len(message)):
                                     result = result + chr(ord(message[i]) + 2)

                                     print (result + '\n\n' )
                                     result = '' 

                                 elif choice is  != 0:
                                     print ("you have entered an invalid command please try again \n\n")

1 个答案:

答案 0 :(得分:0)

您遇到了一些语法错误,这应该可以解决:

result = ""
message = ""
choice = ""

while choice != '0':
    choice = input("\ndo you want to encrypt or decrypt a message?\nEnter1 to encrypt, 2 to decrypt and 0 to exit. ")

    if choice == '1':
        message = input("\nEnter message for encrpytion: ")
        for i in range(0, len(message)):
            result += chr(ord(message[i]) - 2)
        print(result)
        result = ''

    elif choice == '2':
        message = input ("\nEnter message to be decrypted: ")
        for i in range (0, len(message)):
            result += chr(ord(message[i]) + 2)
        print(result)
        result = ''

    elif choice == '0':
        break
    else:
        print ("you have entered an invalid command please try again")