我的代码在运行时会先打印输出。我希望它仅打印该特定计数所需的内容

时间:2018-08-11 15:39:43

标签: python for-loop encryption while-loop count

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

2 个答案:

答案 0 :(得分:0)

将list_text2放入循环中。

ValueError                                Traceback (most recent call last)
<ipython-input-236-487720f2328b> in <module>()
----> 1 data = pd.read_json('t.json')

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression)
    420         return json_reader
    421
--> 422     result = json_reader.read()
    423     if should_close:
    424         try:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in read(self)
    527             )
    528         else:
--> 529             obj = self._get_object_parser(self.data)
    530         self.close()
    531         return obj

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in _get_object_parser(self, json)
    544         obj = None
    545         if typ == 'frame':
--> 546             obj = FrameParser(json, **kwargs).parse()
    547
    548         if typ == 'series' or obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in parse(self)
    636
    637         else:
--> 638             self._parse_no_numpy()
    639
    640         if self.obj is None:

~\Anaconda3\lib\site-packages\pandas\io\json\json.py in _parse_no_numpy(self)
    851         if orient == "columns":
    852             self.obj = DataFrame(
--> 853                 loads(json, precise_float=self.precise_float), dtype=None)
    854         elif orient == "split":
    855             decoded = {str(k): v for k, v in compat.iteritems(

ValueError: Expected object or value

答案 1 :(得分:0)

我假设您每次都是出于某种原因附加到list_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