Python 代码运行但不要求用户输入,在 Sublime Text 中

Python code runs but doesn't ask for user input, in Sublime Text

这是我程序的第一行:

def main():
    country = input('\nPlease enter the country>   ')

我选择了 python 构建,当我单击 Ctrl-B 时它编译正常,并告诉我它花了多长时间。为什么它不打印该行并要求我输入?

您必须调用您想要查找的主要 function/the 表示法是

if __name__ == '__main__':
    country = input('\nPlease enter the country>   ')

我假设您没有在代码中的任何地方调用函数 main。您可以通过在代码末尾添加 if __name__ == '__main__': main() 来解决此问题。

你应该使用 sublime-repl。 要安装它,您需要可用的包控制 here。 然后打开sublime text.type ctrl-shift-p ,选择sublime-repl-python 这将 运行 一个 sublime text 的解释器。 导入您定义模块的文件 (eg.test.py)。 然后调用 test.main()