我在学校学习python用于计算课程作业和加密相关的任务我和一位朋友出现了一个相当奇怪的错误: 我们每个人都有自己的程序副本,其中一个使用"或"在决定程序是加密还是解密的if语句中,这是唯一的区别,有没有人知道为什么这应该影响程序? 该程序打算从用户那里获取一个输入,然后返回加密的字符串,其中每个字符在字母表中移动了5个字母,解密选项与此相反,工作程序和非工作程序都在下面:
工作计划
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)
不工作计划
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)
它似乎只是对问题代码不起作用的加密,它很容易解密工作代码返回的内容。
提前感谢您的任何帮助。)
答案 0 :(得分:2)
if mode == "decrypt" or "d":
始终为True,您可能意味着
if mode == "decrypt" or mode == "d":
所以总是采取第一个分支。同样适用于
elif mode == "encrypt" or mode == "e":
答案 1 :(得分:1)
if mode == 'encrypt' or 'e':
将始终评估为True
,因为'e'
为True
您需要使用:
if mode == 'encrypt' or mode == 'e':
或进行更全面的检查:
if mode.lower().strip() in ['encrypt', 'e']: