简体   繁体   English

两个几乎相同的python文件提供不同的输出

[英]Two almost identical python files giving different outputs

I'm learning python at school for computing coursework and on an encryption related task me and a friend have come on a rather curious error: We each have our own copy of the program, one of which uses an "or" in the if statement that decides whether the program is encrypting or decrypting, this is the only difference, has anyone got any idea as to why this should be affecting the program? 我在学校学习python来计算课程作业,并且在与加密有关的任务上,我和一个朋友遇到了一个非常奇怪的错误:我们每个人都有自己的程序副本,其中一个在if语句中使用“或”决定程序是加密还是解密,这是唯一的区别,是否有人知道为什么这会影响程序? The program intends to take an input from the user and then return the encrypted string where each character has been moved 5 letters along in the alphabet, the decryption option does the inverse of this, both the working and none working programs are below: 该程序打算从用户处获取一个输入,然后返回加密的字符串,其中每个字符在字母表中移动了5个字母,decryption选项执行的操作与此相反,工作程序和无工作程序都在下面:

Working Program 工作程序

mode = input("Do you want to encrypt or decrypt\t")
x=1
while x == 1:
    Raw_input = input('Write Text: ')
    Raw_input = Raw_input.lower()
    output = []
    printOut = str()
    if mode == "decrypt":
        for character in Raw_input:
            number = ord(character) + 91
            if number < 193:
                number += 26
            output.append(number)
        length = len(output)
        for counter in range (length):
            character = chr((output[counter])-96) 
            printOut = printOut + character
    elif mode == "encrypt":
        for character in Raw_input:
            number = ord(character) - 91
            if number > 26:
                number -= 26
            output.append(number)
        length = len(output)
        for counter in range (length):
            character = chr((output[counter])+96) 
            printOut = printOut + character
    print(printOut)

Not Working Program 无效的程序

mode = input("Do you want to encrypt or decrypt\t")
x=1
while x == 1:
    Raw_input = input('Write Text: ')
    Raw_input = Raw_input.lower()
    output = []
    printOut = str()
    if mode == "decrypt" or "d":
        for character in Raw_input:
            number = ord(character) + 91
            if number < 193:
                number += 26
            output.append(number)
        length = len(output)
        for counter in range (length):
            character = chr((output[counter])-96) 
            printOut = printOut + character
    elif mode == "encrypt" or "e":
        for character in Raw_input:
            number = ord(character) - 91
            if number > 26:
                number -= 26
            output.append(number)
        length = len(output)
        for counter in range (length):
            character = chr((output[counter])+96) 
            printOut = printOut + character
    print(printOut)

It only Appears to be the encryption that doesnt work on the problem code, it will easily decrypt what the working code returns. 看来只是对问题代码不起作用的加密,它将很容易解密工作代码返回的内容。

Thanks in advance for any help Given :) 在此先感谢您的帮助:)

if mode == "decrypt" or "d":

is always True, you probably mean 永远是真的,你可能是说

if mode == "decrypt" or mode == "d":

so the first branch is always taken. 因此,始终采用第一个分支。 Likewise for 同样的

elif mode == "encrypt" or mode == "e":

if mode == 'encrypt' or 'e': Will always evaluate to True because 'e' is True if mode == 'encrypt' or 'e':将始终评估为True ,因为'e'True

You need to be using: 您需要使用:

if mode == 'encrypt' or mode == 'e':

or for a more comprehensive check: 或更全面的检查:

if mode.lower().strip() in ['encrypt', 'e']: 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM