【问题标题】:Can execute a program with my IDE but not by doubleclicking when a module is imported可以使用我的 IDE 执行程序,但不能通过在导入模块时双击来执行
【发布时间】:2020-11-20 15:39:32
【问题描述】:

我尝试执行两个脚本,一个带有导入的模块,一个没有。

我双击带有模块的那个,一个窗口出现并迅速消失,然后我尝试另一个没有,它起作用了。

有人可以帮我弄清楚吗?谢谢。

精度:我已经尝试了其他模块的其他脚本,但它不起作用,两个脚本都没有任何错误。 我在 Windows 10 上,两个脚本都是打印给定数字的主要因素,一个带有数学模块,另一个没有。 我仍然可以使用我的 IDE 执行带有模块的那个,但我不想打开我的 IDE 来执行带有模块的脚本。

如果你想要脚本:

# The one without the module ik it's not optimized
num = 56475871
nvNum = 2
nbsPmrs = []
res = []

for nvNum in range(num):
    if not nvNum % 2 == 0 and not nvNum % 3 == 0 and not nvNum % 5 == 0 and not nvNum == 1 or nvNum == 2 or nvNum == 3 or nvNum == 5:
        nbsPmrs.append(nvNum)

for a in range(1, len(nbsPmrs), 1):
    if num % nbsPmrs[a] == 0:
        res.append(nbsPmrs[a])

print(res)
# -----------------------------------------------------
# The one with the module

from math import *

num = 56475871
res = []
t = int(sqrt(num))
for i in range(t):
    while num % 2 == 0:
        num //= 2
        res.append(2)
        if num % 2 != 0:
            break
for a in range(3, t, 1):
    if num % a == 0:
        num //= a
        res.append(a)

res.append(num)
print(res)

【问题讨论】:

  • 细节太少,无法帮助您。您能否制作一个具有相同行为的非常小的程序,并将其发布在这里。另外,请指定操作系统。
  • @Stefan 我在windows上,基本上它在打印一个数字的质因数,使用的模块是数学来计算一个数字的平方根
  • 你能提供代码吗?我认为这是从 IDE 或命令行运行时显示的简单粗壮示例,从 Windows 双击文件时不会发生这种情况
  • @LouieC 我编辑了原文,所以有代码

标签: python module


【解决方案1】:

正如预期的那样,这是脚本完成后打印结果并关闭窗口的情况;与导入无关

要保持窗口打开,您只需将以下行添加到脚本末尾

input('Press Enter to quit')

您还缺少第二个脚本中的最终打印语句;因此没有输出

我让它按预期工作:

from math import *

num = 56475871
res = []
t = int(sqrt(num))
for i in range(t):
    while num % 2 == 0:
        num //= 2
        res.append(2)
        if num % 2 != 0:
            break
for a in range(3, t, 1):
    if num % a == 0:
        num //= a
        res.append(a)

res.append(num)
print(res)
input('Press Enter to quit')

【讨论】:

  • ;-;我按照你说的做了,我有点傻,谢谢你的回答!
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2013-07-29
  • 2014-04-15
  • 1970-01-01
  • 2021-06-05
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多