Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中为Vigenere密码组合字符串和整数?_Python_Vigenere - Fatal编程技术网

在python中为Vigenere密码组合字符串和整数?

在python中为Vigenere密码组合字符串和整数?,python,vigenere,Python,Vigenere,我正在尝试用python编写一个vigenere密码加密程序。我又犯了一个错误 def vigenere(string,key): for i in range(len(key)): if key[i].isupper(): tempList = list(key) tempList[i] = chr(ord(key[i])-65) key = "".join(tempList) elif key[i].islower():

我正在尝试用python编写一个vigenere密码加密程序。我又犯了一个错误

def vigenere(string,key):
for i in range(len(key)):
    if key[i].isupper():
        tempList = list(key)
        tempList[i] = chr(ord(key[i])-65)
        key = "".join(tempList)
    elif key[i].islower():
        tempList = list(key)
        tempList[i] = chr(ord(key[i])-97)
        key = "".join(tempList)
k = 0
newstring = ''
for i in string:
    if i.isupper():
        newstring = newstring + ((ord(i)-65)+(key[k % len(key)]))%26 + 65
    elif i.islower():
        newstring = newstring + ((ord(i)-97)+(key[k % len(key)]))%26 + 97
    k = k + 1
return newstring
+:“int”和“str”不支持的操作数类型-有帮助吗?

首先,您需要更改:

key[i] + ord(key[i])-97
致:

这似乎是打字错误

第二,ord。。。函数返回一个int。要使用chr将其转换回char…:

最后,在Python中,字符串是不可变的。这意味着您不能更改字符串的单个字符。这是一种简单的方法:

if key[i].isupper():
    tempList = list(key)
    tempList[i] = chr(ord(key[i])-65)
    key = "".join(tempList)
elif key[i].islower():
    tempList = list(key)
    tempList[i] = chr(ord(key[i])-97)
    key = "".join(tempList)

ordi-65+key[k%lenkey]%26+65将整数添加到字符串中;这个表达式看起来有点不成熟。如果你给我们输入和预期的输出,顺便说一句。哦,还有一个完整的回溯。key[i]+ordkey[i]-97;这不应该是key[i]=ordkey[i]-97吗?@MartijnPieters我想他在发布之前并没有仔细检查过他的代码|=^/@MartijnPieters感谢您指出这一点-现在我得到一个错误“str”对象不支持项分配。至于输入和输出,它是一个维格纳密码,所以当输入为维格纳EAAAAA,abc时,输出应该是abcabc。你能帮我做第二个for循环吗。如何修复将整数添加到字符串的问题?我该怎么做呢?抱歉,编程新手。谢谢!因此,我修复了输入错误,使用了chr函数,并将新密钥设置为新字符串keyn,随后在函数中将加密字符串设置为newstring。现在我得到了一个错误:不支持+的操作数类型:'int'和'str'这意味着什么,我该如何修复它?谢谢@CyberneticTwekGuruOrc@user3342048编译器应该告诉您错误发生在哪一行。看那条线。这与本文中解决的问题非常相似。提示:仔细阅读错误消息并查看ord…我的编译器说第14行有问题。我想我明白你的意思了…我正在尝试将newstring,一个字符串,与一个ASCII数字连接起来,我需要将ASCII数字转换为它对应的字符。1.这是正确的吗?2.我该怎么做?我似乎找不到如何将ASCII转换为字符,例如65转换为“A”。@user3342048如果您想将ASCII int值转换为字符,请使用chr。。。回答中提到的函数是的,我试过了,但得到了错误消息-不支持+的操作数类型:'int'和'str'
key[i] = chr(ord(key[i])-97)
if key[i].isupper():
    tempList = list(key)
    tempList[i] = chr(ord(key[i])-65)
    key = "".join(tempList)
elif key[i].islower():
    tempList = list(key)
    tempList[i] = chr(ord(key[i])-97)
    key = "".join(tempList)