我正在试着写一个替换加密程序,它根据一个随机数来改变字母表。这是逐个字母或逐个字符完成的
这就是我得到的错误。
File "./encrypt.py", line 35, in <module>
print("after encryption: ", encrypt(text))
File "./encrypt.py", line 26, in encrypt
cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65) TypeError: unsupported operand type(s) for +: 'int' and 'str'有什么解决方案吗?
import random
text = input("enter string: ")
number = 0
number += len(text)
def encrypt(string):
def otp(filename):
file = open(filename + ".txt", "a")
file.write(str(function1()) + "\n")
file.close()
def function1():
for num in range(number):
shift = str(random.randint(1,26))
print(shift)
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65)
otp("onetimepad")
elif char.isupper():
cipher = cipher + chr((ord(char) + str(function1()) - 65) % 27 + 65)
otp("onetimepad")
else:
cipher = cipher + chr((ord(char) + str(function1()) - 97) % 27 + 97)
otp("onetimepad")
return cipher
print("original string: ", text)
print("after encryption: ", encrypt(text))发布于 2020-10-01 07:14:50
错误消息非常清楚。所引用的代码行只有两个+操作,第一个操作显然违反了+的可用定义
ord(char) + str(function1())在左边有一个整数;在右边,您显式地将返回值转换为字符串。确定你想要做什么,并修复你的表达。看起来你正在尝试对一个新的序数值进行整数计算。解决方案可能只是简单地不转换函数的返回值。
https://stackoverflow.com/questions/64146968
复制相似问题