这个 Vigenere 密码中的“字符串索引超出范围”是啥意思?
Posted
技术标签:
【中文标题】这个 Vigenere 密码中的“字符串索引超出范围”是啥意思?【英文标题】:What does "string index out of range" mean in this Vigenere cipher?这个 Vigenere 密码中的“字符串索引超出范围”是什么意思? 【发布时间】:2015-11-11 20:19:30 【问题描述】:#Vigenere Cipher
EncryptedMessage=""
Mode=""
Keywordcount = 0 #sets everything to 0
KeywordTwocount = 0
while Mode != "encrypt" and Mode != "decrypt":
Mode = input('Do you want to encrypt or decrypt a message?')
Mode = Mode.lower()
if Mode == "encrypt":
Message=input('Please eneter the message you wish to encrypt: ')
Keyword=input('Please enter the keyword you want to use:')
KeywordTwo = input(' Please enter second keyword you want to use:')
Keywordlength = len(Keyword)
KeywordTwolength = len(KeywordTwo)
for i in range(0, len(Message)):
Messageencrypt = Message[i]
Keywordencrypt = Keyword[Keywordcount]
KeywordTwoencrypt= KeywordTwo[KeywordTwocount]
if Messageencrypt.isalpha():
if Messageencrypt.isupper():
Messageencrypt = ord(Messageencrypt)
Keywordencrypt = Keywordencrypt.upper()
Keywordencrypt = ord(Keywordencrypt) - 64
KeywordTwoencrypt = KeywordTwoencrypt.upper()
KeywordTwoencrypt = ord(Keyword2encrypt) - 64
FirstCodedMessage = Messageencrypt + Keywordencrypt
FinalCodedMessage = FirstCodedMessage + Keyword2encrypt
if FinalCodedMessage > 90:
FinalCodedMessage = FinalCodedMessage - 26
Encryptedletter = chr(FinalCodedMessage)
elif Messageencrypt.islower():
Messageencrypt = ord(Messageencrypt)
Keywordencrypt = Keywordencrypt.upper()
Keywordencrypt = ord(Keywordencrypt) - 64
KeywordTwoencrypt = KeywordTwoencrypt.upper()
KeywordTwoencrypt = ord(KeywordTwoencrypt) - 64
FinalCodedMessage = Messageencrypt + Keywordencrypt
if FinalCodedMessage > 122:
FinalCodedMessage = FinalCodedMessage - 26
Encryptedletter = chr(FinalCodedMessage)
else:
Encryptedletter = Messageencrypt
EncryptedMessage = EncryptedMessage + Encryptedletter
Keywordcount = Keywordcount + 1
if Keywordcount >= Keywordlength:
Keywordcount = 0
KeywordTwocount = KeywordTwocount + 1
if KeywordTwocount >= Keywordlength:
KeywordTwocount = 0
print(EncryptedMessage)
做什么
KeywordTwoencrypt= KeywordTwo[KeywordTwocount] IndexError:字符串索引超出范围是什么意思? Error
另外,我如何将此加密消息保存为 txt 文件? Error
【问题讨论】:
是时候学习以可调用方式封装部分代码的函数了。 我不知道这是什么意思,我是 python 的初学者,所以………… 我了解到您是初学者。这就是为什么我建议你学习functions,这样你就可以用不同的参数调用相同的代码,而无需复制它。 好的,如果卡住我会再评论 我设法做到了,但知道是说“KeywordTwoencrypt= Keyword2[KeywordTwocount] NameError: name 'Keyword2' is not defined” 【参考方案1】:if KeywordTwocount >= Keywordlength:
应该是if KeywordTwocount >= KeywordTwolength:
如果存在索引问题,那么您应该仔细查看处理此特定索引递增/递减的所有代码部分。
在这种特定情况下,您应该看到第二个关键字的索引不应该与第一个关键字的长度有任何关系。所以更新是错误的:
if KeywordTwocount >= Keywordlength:
KeywordTwocount = 0
通过定义一个使用一个关键字加密消息的函数,可以轻松避免此错误:
def vigenere(message, keyword):
...
然后你可以将这两个链接在一起:
print(vigenere(vigenere(message, keyword1), keyword2))
使用内置函数也可以轻松写入文件:
with open('ciphertext.txt', 'w') as ct:
ct.write(EncryptedMessage)
【讨论】:
【参考方案2】:KeywordTwo[KeywordTwoCount] IndexError: string index out of range
表示索引 (KeywordTwoCount
) 大于您要索引到 KeywordTwo
的字符串的长度。
【讨论】:
我赶时间没时间以上是关于这个 Vigenere 密码中的“字符串索引超出范围”是啥意思?的主要内容,如果未能解决你的问题,请参考以下文章