如何防止IDLE提前运行Python代码

huangapple go评论56阅读模式
英文:

How to prevent IDLE from running a Python code prematurely

问题

我在使用IDLE方面相对新手,是的,这是一个初学者问题,请见谅。我有这段复杂的Python代码(我将嵌入在下面),我正在逐行复制到IDLE中。问题是,在我输入整个代码之前,IDLE程序会运行代码的某些部分。这发生在我在代码的某个部分跳过两行时。当我按原样复制相同的代码并将其输入到在线Python解释器之一时,它可以正常运行,输出完整,而在IDLE中,输出是分散的或不完整。我如何阻止IDLE过早运行代码?在跳过两行后(以便我返回到初始/默认缩进,以“>>>”开头),代码在第25行(counter += 1)运行。以下是代码:

decimal_numbers = [596.8, 596, 596.98, 453.78, 890.99, 435, 234.67, 234, 45.77, 345.99, 654.4, 234, 3245, 642.22]

decimal_parts = []
for num in decimal_numbers:
    decimal_part = str(num).split('.')[1] if '.' in str(num) else '00'
    if len(decimal_part) == 1:
        decimal_part += '0'
    decimal_parts.append(decimal_part)

lst = list(map(int, ",".join(decimal_parts).split(',')))
start_index = 0
counter = 1
grouped_lists = []

for i, num in enumerate(lst):
    if num == 0:
        start_index = i
        print(f"Position: {i + 1}")
        sub_list = lst[start_index:] + lst[:start_index]
        four_index = sub_list.index(0)
        last_digit_list = [num % 10 for num in sub_list[four_index:]]
        print(f"List {counter}: {last_digit_list}")
        grouped_lists.append(last_digit_list)
        counter += 1

matches = {}
for i, sub_list in enumerate(grouped_lists):
    for j, num in enumerate(sub_list):
        if num not in matches:
            matches[num] = [i]
        else:
            matches[num].append(i)

for num, match_indices in matches.items():
    if len(match_indices) > 1:
        print(f"Matches found for number {num} in lists: {match_indices}")

尽管代码提前运行,但我继续输入其余部分的代码,然后在生成输出后,我将继续输入其他部分,但我不想从代码的各个部分获取输出片段。我想能够输入整个代码,然后获得完整的输出,一次性完成。无论如何,在"counter +="行之后,还有一段代码部分我需要在以">>>"开头的新行上继续输入,但由于代码运行,我无法到达这一行。

英文:

I am quite new in using IDLE, and yes this is a rookie question, but please bear with me. I have this long, complex python code (I will embed below), that I am copying line by line to IDLE. The problem is that IDLE program runs some parts of the code before I am done typing the whole code. This happens when I skip two lines at a certain section of the code. When I copy the same code as is and input it in one of the online Python interpreters, it runs just fine and the output is complete, unlike with IDLE, where it is in bits or incomplete. How do I stop IDLE from running the code early? The code runs at line 25 (counter += 1) after skipping two lines (so that I get back to the initial/default indentation starting with ">>>"). Here is the code:


decimal_parts = []
for num in decimal_numbers:
    decimal_part = str(num).split('.')[1] if '.' in str(num) else '00'
    if len(decimal_part) == 1:
        decimal_part += '0'
    decimal_parts.append(decimal_part)

lst = list(map(int, ",".join(decimal_parts).split(',')))
start_index = 0
counter = 1
grouped_lists = []

for i, num in enumerate(lst):
    if num == 0:
        start_index = i
        print(f"Position: {i + 1}")
        sub_list = lst[start_index:] + lst[:start_index]
        four_index = sub_list.index(0)
        last_digit_list = [num % 10 for num in sub_list[four_index:]]
        print(f"List {counter}: {last_digit_list}")
        grouped_lists.append(last_digit_list)
        counter += 1

matches = {}
for i, sub_list in enumerate(grouped_lists):
    for j, num in enumerate(sub_list):
        if num not in matches:
            matches[num] = [i]
        else:
            matches[num].append(i)

for num, match_indices in matches.items():
    if len(match_indices) > 1:
        print(f"Matches found for number {num} in lists: {match_indices}")```


Irrespective of the code running prematurely, I continued inputting the rest of the code, then after the output was generated, I would continue typing the other sections, however, I don't want bits of outputs from bits of code. I want to be able to type the whole code, and get the whole output, all in once. Anyways, after "counter +=" line, there is another section of the code I still need to type on a new line starting with ">>>", but I never get to this line as the code runs.



</details>


# 答案1
**得分**: 1

IDLE shell 只是一个解释器,您可以在新行开始时使用 <kbd>Shift + Enter</kbd> 一次性发送多行代码块,但建议运行 Python 文件。要执行此操作,您可以通过按下 <kbd>Control + N</kbd> 或转到 *文件* -> *新建文件* 创建新文件。现在将所有文本复制到该新文件中,然后通过按下 <kbd>F5</kbd> 或转到 *运行* -> *运行模块* 来运行它。如果它要求您首先保存文件,如果是这样,请按 *确定* 并选择一个位置。

<details>
<summary>英文:</summary>

The IDLE shell is just an interpreter, you can send multi-line blocks of code at once by using &lt;kbd&gt;Shift + Enter&lt;/kbd&gt; when starting a new line, but instead it is recommended to run a Python file instead. To do this you make a new file by pressing &lt;kbd&gt;Control + N&lt;/kbd&gt; or going to *File* -&gt; *New file*. Now copy all text into that new file and run it by pressing &lt;kbd&gt;F5&lt;/kbd&gt; or by going to *Run* -&gt; *Run module*. It may ask you to save your file first, if it does, press *Ok* and select a location.

</details>



# 答案2
**得分**: -3

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

"What version of IDLE are you using? You might be needing to update it to the latest version. Also, be wary of indentation and spacing. You can adjust indentation on IDLE's general settings and switch off automatic indentations... hope this helps."

<details>
<summary>英文:</summary>

What version of IDLE are you using? You might be needing to update it to the latest version. Also, be wary of indentation and spacing. You can adjust indentation on IDLE&#39;s general settings and switch off automatic indentations... hope this helps.

</details>



huangapple
  • 本文由 发表于 2023年2月23日 19:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定