请求用户输入,如果是则执行功能,如果否则退出程序

2024-04-26 06:07:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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语句时,我得到错误消息:无效语法。我知道我想得太多了。你知道吗


Tags: thetoinputifmaincodechthis
1条回答
网友
1楼 · 发布于 2024-04-26 06:07:15

我看到代码中有几个问题:

  1. 有一个.lower调用缺少括号(请注意,没有paren的'xyz'.lower始终计算为True
  2. print(plainText)需要缩进
  3. elif分支需要与它所使用的if分支对齐
  4. code = code什么都不做
  5. 不应该重用plainText变量;相反,应该使用一个新变量,例如decodedText

相关问题 更多 >