【问题标题】:Python program will run, but won't be kept open for me to look at the outputPython 程序将运行,但不会保持打开状态让我查看输出
【发布时间】:2017-12-30 21:00:55
【问题描述】:

我是 Python 新手,刚刚开始编写自己的程序。

我有一些使用 IDLE 创建的程序。这些程序会运行,但输出会在屏幕上显示大约 1/4 秒,然后才会消失...

我在互联网上进行了搜索,但没有找到解决方案...有人可以解释为什么会发生这种情况,以及如何“应对”它吗?

注意:我是双击文件

谢谢!

代码示例:

def loader():
percentage = float(input("what percentage do you want?"))
x = 0
print("\nGoal:{} %".format(percentage))


if percentage <= 100 and percentage > 0:
    while x < percentage:
        x+=1
        print("Loading...{}%".format(x))

else:
    print ("Error, you can't overload/underload")


loader()


def trinomial():

a = float(input("a?"))
b = float(input("b?"))
c = float(input("c?"))

print("\n a = {}\n b = {} \n c = {}".format(a,b,c))
print("So you are trying to find x for \n {}x^2 + ({}x) + ({})".format(a,b,c))

delta = b**2 - (4 * a * c)

if delta > 0:
    x1 = (-b - sqrt(delta))/(2*a)
    x2 = (-b + sqrt(delta))/(2*a)
    print("For x = {} or x = {}, the trinomial is solved".format(x1, x2))

elif delta < 0:
    print ("No values of x possible")

elif delta == 0: 
    #I know, I could have used "else"
    x1 = -b/(2*a)
    print("For x = {} the trinomial is solved".format(x1)) 

trinomial()

【问题讨论】:

  • 你是如何运行程序的?通过空闲?通过终端?还是双击脚本?
  • @AdamSmith 双击
  • 请分享程序代码
  • 你的代码在哪里?
  • 这里的代码实际上几乎是无关紧要的。 (或者我应该说可能几乎无关紧要!)

标签: python python-3.x output


【解决方案1】:

您提供的信息太少,但可能不是 Python 的问题,而是 Windows 控制台的问题。从 cmd 应用程序打开您的程序,或等到按下 Enter 键。将其放在脚本的末尾。

sys.stdin.read(1)

【讨论】:

  • import sys 开头。抱歉,忘记了这个
【解决方案2】:

当您双击脚本时,它会启动 python 的新实例,打开绑定到该实例的终端窗口,通过它运行脚本(管道输出并侦听该终端上的输入),然后关闭脚本结束时的终端和实例。

阻止这种情况的简单方法是从现有 shell 中调用脚本。使用python your_script_location 运行。

或者,您可以在代码末尾添加用户输入请求,例如:

# after all your other code, just before execution falls off the bottom...
input("Press enter to close...")

【讨论】:

  • @FlipFloop 这更像是一个用户问题而不是程序员问题。您使用的是 Windows、Mac 还是 Linux?从您使用的任何操作系统启动一个外壳(Windows 是 Win+R ->“cmd”,Mac 我认为您必须在 Spotlight 中搜索“终端”,尽管可能有一些快捷方式。至少,Ubuntu 让你使用 ctrl+alt+T) 然后在 shell 中输入。
  • 在 Windows 上你应该更喜欢py -3 your_script_location。 Mac 和 Linux 使用python3 your_script_location。注:在 Mac 或 Linux 上,python scriptname.py 几乎肯定会运行 Python2。
猜你喜欢
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多