询问用户输入,如果是,则执行功能,如果没有退出程序

时间:2017-10-24 18:37:49

标签: python function input

def main():
    plainText = input("Enter a one-word, lowercase message: ")
    distance = int(input("Enter the distance value: "))
    code = ""
    for ch in plainText:
        ordValue = ord(ch)
        cipherValue = ordValue + distance
        if cipherValue > ord('z'):
            cipherValue = ord('a') + distance - (ord('z') - ordValue + 1)
        code +=  chr(cipherValue)
    print(code)

    userDecrypt = input("Would you like to decrypt the text? ").lower

    # i want this yes answer to force the next few lines of code to run and if no then it quits the program
    if userDecrypt == 'yes' or userDecrypt == 'y':
        code = code
        plainText = ""
        for ch in code:
            ordValue = ord(ch)
            cipherValue = ordValue - distance
            if cipherValue < ord('a'):
                cipherValue = ord('z') - (distance - (ord('a') - ordValue + 1))
            plainText += chr(cipherValue)
    print(plainText)

        # this is where my problem lies. It won't allow this
        elif userDecrypt == 'no' or userDecrypt == 'n': 
            print("Have a good day")

main()

elif语句是我的问题。我希望能够运行解密代码,如果用户选择是,如果没有,那么我希望程序退出。如果是的话,我可以让它解码但是当我添加else语句时,我收到错误消息:语法无效。我知道我在想这个。

1 个答案:

答案 0 :(得分:0)

我在发布的代码中看到了几个问题:

  1. .lower调用缺少括号(请注意'xyz'.lower没有parens 总是评估为True
  2. print(plainText)需要缩进
  3. elif分支需要缩进以与if分支对齐
  4. code = code没有做任何事情
  5. plainText变量不应重复使用;相反,您应该使用新的变量,例如decodedText