- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试制作维吉尼亚密码。当我尝试加密消息时,出现以下错误。
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
ValueError: tuple.index(x): x not in tuple
我不确定是什么问题导致了错误,有什么帮助吗?
baseAlphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
plainText = input("Please enter the plain text")
key = input("Please enter the key word")
keyList = []
keyLength = 0
while keyLength < len(plainText):
#Adds the users entered key into a list character by character.
#Also makes the key the same length as plainText
for char in key:
if keyLength < len(plainText):
keyList.append(str(char))
keyLength = keyLength + 1
#The variable each processed letter is appended to
completeCipherText = []
#This is the value used to temporaily store the ciphertext character during the iteration
cipherCharIndexValue = 0
keyIncrement = 0
#iterates through the plain text
for plainTextChar in plainText:
#Adds the base alphabets index value of the key and the plain text char
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
while cipherCharIndexValue > 25:
#makes the addition value under 26 as to not go out of range of base alphabet tuple
cipherCharIndexValue = cipherCharIndexValue - 26
#appends the ciphertext character to the completeCipherText variable.
#The character is the index of the key + index of the plainTextChar from baseAlphabet
completeCipherText.append(baseAlphabet[cipherCharIndexValue])
#Moves onto the next key
keyIncrement = keyIncrement + 1
print ('').join(completeCipherText)#Makes the result a strings for printing to the console.
最佳答案
看来您使用的是python2.x,您应该使用raw_input
而不是input
。
如果您的输入字符串中有空格或其他标点符号,您的代码将会崩溃,因此我建议您先确保 keyList[keyIncrement]
位于 baseAlphabet
中你使用 index
方法,如果它不在这个元组中,你会得到这个错误:
ValueError: tuple.index(x): x not in tuple
例如
if keyList[keyIncrement] in keyList:
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
或者您可以使用 try/catch
捕获异常以调试代码。
希望这有帮助。
关于Python Vigenere 元组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858038/
我正在查看 http://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Java 上提供的 Vigene Ciphere 源代码.我尝试自己测试该程序,但它并没有
我刚刚解决了CS50中的Vigenere问题,但是仍然只有一个错误,即非字母字符,当你用纯文本编写任何没有空格、逗号、任何非字母的内容时,程序将运行良好,但是如果您写了任何非字母字符,例如空格,下一个
我在使用 Vigenere 时遇到了一些问题,需要一些帮助。 /* This program is a Vigenere Cipher. I am Daniel of Asguard. */ #inc
伙计,我以为我已经拥有了!我一直在研究 Vigenere 问题并且已经接近了,但是当我检查时不断收到此错误。当 key 必须循环回来时,看起来好像有问题。想法? 这是错误: :) vigenere.c
我编写了一个 Java 程序,该程序使用 Vigenere 密码进行编码,加密工作正常,但解密不适用于某些特殊情况。 例如,如果明文为“k”且 key 为“y”,则它会正确生成密文“i”((10 +
任务是在运行程序时给出关键字参数,我将其保存为string k,然后用户输入文本,程序将根据关键字输出输入的文本。 A = 0,B = 1,所以如果关键字是 ABABA,文本是 hello,它会输出
我原来有一个凯撒的密码,看起来是这样的: #include #include int main (int argc, char const *argv[]) { printf("I
所以我的老师创造了这个 vigenere 密码,他说它可以工作。然而,在使用在线 vigenere 密码检查其结果后,它似乎不是正确加密的结果。 我不知道如何修复它,我想知道是否有人可以指导我找出错误
我正在编写 Vigenere Cipher 作为 CS50 的一部分。这是我的代码。 #include #include #include #include #include int main(int
我正在尝试制作维吉尼亚密码。当我尝试加密消息时,出现以下错误。 cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement])
我是编程新手。这是我到目前为止编写的代码。忽略加密本身的细节;我知道这需要更多的工作。当我尝试运行该程序时,收到段错误错误消息。如果 argc != 2 我会收到消息,如果 argc == 2 它会打
我正在参加 Edx 上的在线类(class) cs50,我有一个作业,其中我必须创建一个程序,用户在其中输入关键字(然后用于加密)和需要在 Vigenere 中加密的字符串。密码。 Vigenere
我不明白为什么它不起作用。当有 3 个或更多参数时它会提示,但当只有一个 Vigenere 参数时它不会提示。我看过其他有同样问题的人,他们说这可以解决......不知道我在这里缺少什么。当我运行 .
我正在尝试执行CS50 Vigenere exercise . #include #include #include #include #include int main(int argc,
我正在制作一个解密维吉尼亚密码的程序。用户只能给出字母键。 for (int i = 0, counter = strlen(text); i < counter; i++) {
首先我没有人可以问这种问题所以请原谅我 #include #include #include #include #include int main(int argc, string argv
#include #include #include #include #include int main(int argc, string argv[]) { // two arg
我已经制作了加密和解密 Vigenere 密码的程序,但我有几个问题。 这是一个:句子的第一个字母加密不正确。 第二个:在句子后我有字母 K。我认为这是空间原因,但我不知道如何解决。 第三个问题:加密
我正在尝试编写一个读入(-e 加密,-d 解密)、一个关键字(在加密期间使用)、原始消息来自的文本文件和另一个文本的 Vigenere Cipher加密/解密消息输出到的文件,全部来自命令行参数。我对
我正在用 C 语言实现 Vigenere 密码。我的解决方案一直错误地加密纯文本。因此,我决定对我的代码进行(有点武断的)更改。更改的目的只是通过更好地定位我的变量并为它们提供更合适的名称来使代码更具
我是一名优秀的程序员,十分优秀!