Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 我应该使用什么类型来代替int或str?_Python_Encryption - Fatal编程技术网

Python 我应该使用什么类型来代替int或str?

Python 我应该使用什么类型来代替int或str?,python,encryption,Python,Encryption,首先让我解释一下代码:我的代码是一个加密代码。首先它给出两个数字,然后它有两个加密阶段。 第一阶段,将反转文本,直到其索引与第一个数字相等的字符。 第二阶段,将前一阶段输出的字符在其新位置的数量乘以第二个数字。最后打印加密的内容。 在这段代码中,我对第7行代码有问题。 不知道第7行中的变量应该使用什么类型。无论我使用str还是int,我都会收到错误消息 first_num=int(input("Fnum: ")) second_num=input("Snum: ") encrypt_stage1

首先让我解释一下代码:我的代码是一个加密代码。首先它给出两个数字,然后它有两个加密阶段。 第一阶段,将反转文本,直到其索引与第一个数字相等的字符。 第二阶段,将前一阶段输出的字符在其新位置的数量乘以第二个数字。最后打印加密的内容。 在这段代码中,我对第7行代码有问题。 不知道第7行中的变量应该使用什么类型。无论我使用str还是int,我都会收到错误消息

first_num=int(input("Fnum: "))
second_num=input("Snum: ")
encrypt_stage1="".join(name[first_num-1::-1])+"".join(name[first_num::])

for place,char in enumerate(encrypt_stage1):
    ascii_code=ord(char)
    encryption_stage2=""
    encryption_stage2 += chr(str(int(ascii_code)+place*second_num))

print(encryption_stage2)
>>>TypeError: unsupported operand type(s) for +: 'int' and 'str'


----------
example of input: name=vahid, first_num=1, second_num=3
output will be: ygqus

至少有三个问题:

  • 您忘记将
    second\u num
    转换为
    int
  • 您无法将
    chr()
    应用于
    str()
    ,我已删除
    chr()
    调用
  • 我必须将
    encryption\u stage2=“
    移动到循环之外,以避免在每次迭代中重新分配它
处理完这些问题后,代码如下所示:

name = 'name'
first_num=int(input("Fnum: "))
second_num=int(input("Snum: "))
encrypt_stage1="".join(name[first_num-1::-1])+"".join(name[first_num::])

encryption_stage2=""
for place,char in enumerate(encrypt_stage1):
    ascii_code=ord(char)
    encryption_stage2 += str(int(ascii_code)+place*second_num)

print(encryption_stage2)

您收到的错误是什么?另外,请明确说明您在
first_num
second_num
中使用了什么,以便我们可以重现错误。我注意到,您不会像对
第一个数值
那样将
第二个数值
转换为
整数
。这可能是问题的一部分。您应该加密字节,而不是字符串或整数。您使用的是哪个Python版本?Python2或Python3?将第二个num转换为int不会导致错误?还有,什么是
name
?请确保您可以准确地运行给定的示例,无需其他代码。谢谢您的帮助。我是python的初学者。但是我没有给出我想要的结果,你可以提供输入
名称
第一个数值
第二个数值
,以及预期的输出,然后有人可以帮助你。