【问题标题】:Stop program on condition python在条件python上停止程序
【发布时间】:2020-04-08 16:56:57
【问题描述】:

我是 python 的新手,我正在工作,因为动手做总是最好的学习方式。我被困在这里,我的逻辑似乎对我知道的小代码没有帮助。

我想在程序满足某个条件后停止它。我在下面给出了一个例子。

ex_list = ["1","2","3"]

print(*ex_list, sep="\n")

预期输出:

1 
2
3

输出:

1
2 
2 is here exiting

这里没有显示2之后的数字3。程序在找到 2 时终止。

我尝试了是否可以访问和处理打印内容,因此我将内容放入变量中并使用if 语句。它没有用...任何帮助将不胜感激。

【问题讨论】:

    标签: python printing auto


    【解决方案1】:

    试试这样的:

    import sys
    ex_list = [1,2,3]
    for e in ex_list:
      print e
      if e == 2:
        print '2 is here! exiting..' 
        sys.exit(0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多