def main():
# open file to be used
inFile = open ('input.txt', 'r')
fileContent = inFile.readline()
choice = input('Do you want to encrypt or decrypt? (E / D): ')
# find even and odd characters in string
if (choice == 'E'):
for i in range(0, len(fileContent), 2):
even_str = fileContent[i]
print(even_str)
for i in range(1, len(fileContent), 2):
odd_str = fileContent[i]
encrypted_str = odd_str + even_str
# open outfile and write new strings in
outFile = open("output.txt", "w")
outFile.write(even_str)
outFile.write(odd_str)
outFile.write(encrypted_str)
outFile.close()
if (choice == 'D'):
half = fileContent // 2
even_str = fileContent[:half]
# return this if user has incorrect input
if (choice != 'E' and choice != 'D'):
print ('')
print ('Wrong input. Bye.')
return
inFile.close()
main()
现在我只是测试看看even_str的输出是什么。输入是“快速的棕色狐狸跳过懒狗。”当我应该得到每个偶数字符时,我为even_str得到的输出是“g”。
答案 0 :(得分:0)
您每次循环都会为even_str分配一个新值,因此当循环完成时,该值将是最后分配的值 - 在这种情况下' g'。如果你想要一个包含所有偶数字符的列表,你需要将它作为一个列表并将每个字符附加到列表中:
even_str = list()
for i in range(0, len(fileContent), 2):
even_str.append(fileContent[i])