Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 如何增加列表中每个项目/元素的值?_Python_List_Python 2.x_Decoder - Fatal编程技术网

Python 如何增加列表中每个项目/元素的值?

Python 如何增加列表中每个项目/元素的值?,python,list,python-2.x,decoder,Python,List,Python 2.x,Decoder,我目前正在尝试制作一个Caesar解码器,因此我试图找出如何获取用户输入的移位值,并使用该输入来移位列表中的每个项目。但每次我尝试,它总是给我一个错误 例如: ASCII中的单词应为: [119, 111, 114, 100] 如果给定的shift输入为2,我希望列表为: [121, 113, 116, 102] 请帮忙。这是我第一次编程,这个凯撒解码器让我发疯:( 这就是我目前所拥有的 import string def main(): inString = raw_input

我目前正在尝试制作一个Caesar解码器,因此我试图找出如何获取用户输入的移位值,并使用该输入来移位列表中的每个项目。但每次我尝试,它总是给我一个错误

例如:

ASCII中的单词应为:

[119, 111, 114, 100]
如果给定的shift输入为
2
,我希望列表为:

[121, 113, 116, 102]
请帮忙。这是我第一次编程,这个凯撒解码器让我发疯:(

这就是我目前所拥有的

import string

def main():

    inString = raw_input("Please enter the word to be "
                        "translated: ")
    key = raw_input("What is the key value or the shift? ")

    toConv = [ord(i) for i in inString] # now want to shift it by key value
    #toConv = [x+key for x in toConv]   # this is not working, error gives 'cannot add int and str

    print "This is toConv", toConv

另外,如果你们不使用任何花哨的函数,这会很有帮助。取而代之的是,请使用现有的代码。我是个新手。

如果你们对单行程序感兴趣:

shifted_word = "".join([chr(ord(letter)+shift_value) for letter in word])

如果您对单班轮有兴趣:

shifted_word = "".join([chr(ord(letter)+shift_value) for letter in word])
返回字符串对象并返回整数。此外,正如错误消息所述,不能将字符串和整数与
+
一起添加:

>>> 'a' + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>
在上面的代码中,
x
将是一个整数(因为
toConv
是一个整数列表),而
key
将是一个字符串(因为您使用了
raw\u input
来获取其值)


只需将输入转换为整数即可解决此问题:

key = int(raw_input("What is the key value or the shift? "))
之后,您的列表理解将正常工作


下面是一个演示:

>>> def main():
...     inString = raw_input("Please enter the word to be "
...                         "translated: ")
...     # Make the input an integer
...     key = int(raw_input("What is the key value or the shift? "))
...     toConv = [ord(i) for i in inString]
...     toConv = [x+key for x in toConv]
...     print "This is toConv", toConv
...
>>> main()
Please enter the word to be translated: word
What is the key value or the shift? 2
This is toConv [121, 113, 116, 102]
>>>
返回字符串对象并返回整数。此外,正如错误消息所述,不能将字符串和整数与
+
一起添加:

>>> 'a' + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>>
在上面的代码中,
x
将是一个整数(因为
toConv
是一个整数列表),而
key
将是一个字符串(因为您使用了
raw\u input
来获取其值)


只需将输入转换为整数即可解决此问题:

key = int(raw_input("What is the key value or the shift? "))
之后,您的列表理解将正常工作


下面是一个演示:

>>> def main():
...     inString = raw_input("Please enter the word to be "
...                         "translated: ")
...     # Make the input an integer
...     key = int(raw_input("What is the key value or the shift? "))
...     toConv = [ord(i) for i in inString]
...     toConv = [x+key for x in toConv]
...     print "This is toConv", toConv
...
>>> main()
Please enter the word to be translated: word
What is the key value or the shift? 2
This is toConv [121, 113, 116, 102]
>>>