我是Python的新手。这是我的第三个项目,我面临一些障碍。在这里,我有一个Caesar密码项目。它似乎做我需要做的一切,只接受大写字母,没有特殊字符,没有小写字母,没有空格。
但是,我有两个问题:
如果有人能提供解决方案或提出解决上述问题的建议,那将是非常有帮助的。非常感谢你的时间和关注!
这是我的代码:
MAX_NUMBER_KEY = 26
def getMode():
while True:
print('Please make your selection from the following "E" for encryption or "D" for decryption:')
mode = raw_input()
if mode in 'E D'.split():
return mode
else:
print('Error! Please try again and make sure to choose only "E" or "D"!')
def getText():
while True:
print('Enter your text that you would like to encrypt or decrypt:')
text = raw_input()
if text.isalpha() and text.isupper():
return text
else:
print('Error! No spaces, no special characters or numbers! Please only use letters!')
def getKey():
key = 0
while True:
print('Enter your number key for "K" (1-%s)' % (MAX_NUMBER_KEY))
key = int(raw_input().isdigit())
if (key >= 1 and key <= MAX_NUMBER_KEY):
return key
else:
print('Error! Please try again and choose only numbers between (1-26)!')
def getEncryptedMessage(mode, text, key):
if mode[0] == 'D':
key = -key
encrypted = ''
for each in text:
if each.isalpha():
num = ord(each)
num += key
if each.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
encrypted += chr(num)
else:
encrypted += each
return encrypted
mode = getMode()
message = getText()
key = getKey()
print('Your encrypted message is:')
print(getEncryptedMessage(mode, message, key))
答案 0 :(得分:2)
在getKey()
中,raw_input().isdigit()
返回一个布尔值,因此通过将其转换为int,您将分别执行int(True)
或int(False)
,分别为1和0