Python “运行时我的代码”在输出之前打印输出。我希望它只打印特定计数所需的内容

Python “运行时我的代码”在输出之前打印输出。我希望它只打印特定计数所需的内容,python,for-loop,encryption,while-loop,count,Python,For Loop,Encryption,While Loop,Count,为什么我的代码在while循环之前打印输出。我只想打印计数和该计数的加密文本。将列表文本2放入循环中 while count < 26: list_text2 = [] for letter in list_text1: 计数

为什么我的代码在while循环之前打印输出。我只想打印计数和该计数的加密文本。

将列表文本2放入循环中

while count < 26:
    list_text2 = []
    for letter in list_text1:
计数<26时:
列表_text2=[]
对于列表_text1中的字母:

我假设您每次都是出于某种原因附加到
列表_test2
,因此,下面的代码不应该把它放在循环之外,而应该将最新的更新从它的尾部切掉,只打印该部分:

import string

text_to_encrypt = ('Please enter some text to encrypt: ')

offset = list(range(1,27))

alphabet = list(string.ascii_lowercase)
list_text2 = []
encrypted_text = ''
list_text1 = list(text_to_encrypt.lower())
adjusted_list = 2*alphabet

count = 1

while count < 26:
    for letter in list_text1:
        if letter not in alphabet:
            list_text2 += letter

        else:
            index = adjusted_list.index(letter)
            list_text2 += adjusted_list[index + offset[count]]

    list_text2_len = len(list_text2)
    slice_start_index = list_text2_len - len(text_to_encrypt)
    print(''.join(list_text2[slice_start_index:list_text2_len]))
    encrypted_text = ''.join(list_text2)
    count += 1
导入字符串
text_to_encrypt=(“请输入一些要加密的文本:”)
偏移量=列表(范围(1,27))
字母表=列表(string.ascii_小写)
列表_text2=[]
加密文本=“”
list_text1=list(text_to_encrypt.lower())
调整后的列表=2*字母表
计数=1
当计数小于26时:
对于列表_text1中的字母:
如果字母不在字母表中:
列表_text2+=字母
其他:
索引=调整后的列表。索引(字母)
列表\文本2+=调整后的\列表[索引+偏移量[计数]]
list_text2_len=len(list_text2)
slice\u start\u index=list\u text2\u len-len(文本加密)
打印(“”.join(列表文本2[切片开始索引:列表文本2\u len]))
加密的文本=''.join(列表文本2)
计数+=1

请修复缩进。另外,是什么让您认为代码在进入
while
循环之前会打印某些内容?我们真的不清楚您预期会发生什么,以及现实情况与您的预期有何不同。请提供示例输入和输出。然后解释你希望在输出中有什么不同。你的循环不断地向
列表\u text2
添加元素,因此输出当然会持续增长。如果那不是你想要的,你为什么要这么做?