Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
尝试对列表进行操作时,Python3.x中出现不支持的操作数类型错误_Python_Python 3.x - Fatal编程技术网

尝试对列表进行操作时,Python3.x中出现不支持的操作数类型错误

尝试对列表进行操作时,Python3.x中出现不支持的操作数类型错误,python,python-3.x,Python,Python 3.x,假设所有ASCII码都设置为变量a=97、b=98等 word = eval(input("What would you like to say? ")) key = 174 print (word) changedword = (', '.join(str(I + key) for I in word)) print ("Your encrypted string is:" + changedword) ans1 = input("Would you like to decrypt this?

假设所有ASCII码都设置为变量a=97、b=98等

word = eval(input("What would you like to say? "))
key = 174
print (word)
changedword = (', '.join(str(I + key) for I in word))
print ("Your encrypted string is:" + changedword)
ans1 = input("Would you like to decrypt this?")
print (ans1)
if (ans1 == "yes"):
    print (changedword)
    decryptedword = (', '.join(str(I - key) for I in changedword))
    ans2 = input(decryptedword + " was your decrypted number list. Do you 
want to translate to ASCII code?")
    if (ans2 == "yes"):
        print (', '.join(str(chr(I)) for I in decryptedword))
当运行这段代码时,我得到了错误

TypeError: unsupported operand type(s) for -: 'str' and 'int'
关于第11行 我知道str和int是不同的,但在我第一次使用它时它就起作用了,我不知道如何解决这个问题。任何帮助都将不胜感激

我知道它经常引用加密,我知道它并不是真正的加密,但我对这一点还不熟悉,我只是在玩


所有额外的打印都是为了我自己的测试。

正如你在问题中所说,问题是:

 decryptedword = (', '.join(str(I - key) for I in changedword))   
key是int,但changedword是此行的str:

changedword = (', '.join(str(I + key) for I in word))
您在changedword中为I迭代changedword,因此I也是str类型

所以当你试图从str中减去int时,你的问题是I-key


如果要使用ascii值进行加减,请使用函数ordc,其中c是字符串中的单个字符。当您想将其转换回字符时,请使用函数chra,其中a是ascii int

strI+键不是有效的操作,因为I是char,key是int。您想用这一行实现什么?@GarbageCollector:strI+键中的I几乎可以是任何东西;他们评估了输入的结果,没有给我们示例输入、预期输出和观察到的最小输出。当他们点击strI-key时,我肯定是str中的一个字符,所以它本身就是str。但是changedword是由int组成的。我仍然理解这个问题,所以我可以将changedword的内容更改为var并让它工作吗?如果是这样的话,我该怎么做呢?如果你知道changedword中的每个字符都是int,你可以用int将其转换为int。但是如果你有非int字符,这可能会导致错误。如果我把它改为int,它会给我TypeError:sequence item 0:expected str instance,int found。所以我建议使用ordc,它会给你ascii int表示。ordc将始终处理角色。ints仅在s是只包含整数的str并且容易出现TypeErrorsoh ok时才起作用。非常感谢@JakeStephens,如果你想把你所说的作为一个正式的回答,我和更多人一定会非常感激。