Python脚本不打印输出

2024-06-06 17:51:23 发布

您现在位置: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下,运行该命令时我在该目录中。我是不是遗漏了什么,为什么这不起作用?


Tags: 文件textpytest命令程序终端input
3条回答

使代码如此,没有函数

x = input("Input something: ")
while (x):
    x = input("Input something: ")

print x

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

这将满足您的期望:

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

    print(x)

GetNum()

我删除了函数参数Text,添加了对GetNum函数的调用,并为这两个input()调用添加了从strint的类型转换。

您尚未调用GetNum函数。

您需要在脚本的底部添加以下内容:

GetNum(None)

Text未使用,因此没有一个是空对象。

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

相关问题 更多 >