【问题标题】:python cesar cipher issues [closed]python cesar密码问题[关闭]
【发布时间】:2019-07-17 17:40:45
【问题描述】:

我是编码新手,一直在尝试制作 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")

【问题讨论】:

  • 请使用跟踪发布确切的错误。为什么print (result + ('\n\n' ) 和它下面的行如此缩进呢? choice is != 0: 也可能是一个错误。看起来应该只是choice != 0:
  • 你在chr(ord(message)[i] - 2和print (result + ('\n\n' )缺少一个结束括号
  • 我不确定我是否关注了这个 youtube 视频 youtube.com/watch?v=-IDU5rQ9q5Y,他们就是这样做的对不起,我是个菜鸟

标签: python syntax-error spyder


【解决方案1】:

你有几个语法错误,这应该可以解决它:

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")

【讨论】:

  • 感谢 merig 解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多