[英]what type should i use instead of int or str?
首先让我解释一下代码:我的代码是一个加密代码。 首先它给出两个数字,然后它有两个加密阶段。 第一阶段,将反转文本直到其索引与第一个数字相等的字符。 第二阶段,将前一阶段输出的字符按其新位置的数量乘以第二个数字。 最后打印加密的内容。 在这段代码中,我对第 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_num
转换为int
chr()
应用于str()
,我删除了chr()
调用encryption_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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.