cjm*_*cjm 1 python if-statement python-3.x
我写了一个非常基本的加密程序,在为它编写解密算法时,我遇到了一些循环问题.
from re import *
cipher = open('cipher.txt')
ciphertext = cipher.read()
keyfile = open('key.txt')
key = keyfile.read()
decoded = []
chardec = ''
inval = 1
print("Decoder for encrypt1.py")
while inval == 1:
useManKey = input("Use a manual key? Y or N\n> ")
if useManKey == 'Y' or 'y':
key = input("Please enter the key you wish to use to decrypt\n> ")
inval = 0
elif useManKey == 'N' or 'n':
inval = 0
print("OK, decrypting")
else:
print("That wasn't a valid option/nPlease re-enter")
Run Code Online (Sandbox Code Playgroud)
当我运行它,并将useManKey声明为N或n时,它似乎运行循环的if部分,就像我已将其声明为Y或y一样.我可能在这里很傻,但是非常感谢任何帮助.
Sil*_*Ray 10
useManKey == 'Y' or 'y'你认为它不起作用.你想要的是什么useManKey in ('Y', 'y').您首先评估的是useManKey == 'Y',如果该检查失败,则测试字符串'y'是否真实.由于非空字符串总是很完整,因此您的if语句始终求值为True.正如评论中指出的那样,如果需要,您还可以使用upper()或lower()首先将输入转换为固定大小写.