Python 脚本不打印输出

新手上路,请多包涵

我有这个简单的 python 程序:

 def GetNum (Text):
    x = input("Input something: ")
    while (x > 0):
        x = input("Input something: ")

    print x

我想通过终端运行它,但是当我执行命令时:

 python ./test.py

或者如果我跑

python test.py

什么都没发生。终端恢复正常,就好像没有执行任何命令一样。

该文件位于 Documents/Python 下,我在运行命令时位于该目录中。关于为什么这不起作用,我在这里遗漏了什么吗?

原文由 Anderology 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 498
2 个回答

您还没有调用您的 GetNum 函数。

您需要将以下内容添加到脚本底部:

 GetNum(None)

Text 没有使用,所以 None 是一个空对象。

您可能想阅读有关定义函数、函数参数和调用函数的内容,这超出了 StackOverflow 的范围 - 请参阅 http://www.tutorialspoint.com/python/python_functions.htm

原文由 Alastair McCormack 发布,翻译遵循 CC BY-SA 3.0 许可协议

您的程序不会输出任何内容,因为您从不调用您的函数。

这将做你所期望的:

 def GetNum():
    x = int(input("Input something: "))
    while (x > 0):
        x = int(input("Input something: "))

    print(x)

GetNum()

I removed the function argument Text , added a call to the GetNum function and added type conversions from str to int for both input() 来电。

原文由 pp_ 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题