![](/img/trans.png)
[英]My code works fine inside the python shell, but not showing anything when running it on my Visual studio code
[英]Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE?
我目前正在学习 Python 并且遇到了一个非常奇怪的问题(至少对我而言)。 当我输入此代码时:
def search4vowels(word):
"""Display any vowels found in a supplied word."""
vowels = set('aeiou')
found = vowels.intersection(set(word))
return bool(found)
在 IDLE 编辑窗口中并使用 IDLE 运行它,一切正常,我需要在提示符下输入我的函数才能启动。
但是,当我在 Visual Studio Code 中编写相同的代码并使用Run Python file in Terminal选项运行它时,我得到的是:
我也尝试过Code Runner扩展,这给了我一个空白的输出页面,并且无法输入函数。
例如: search4vowels('galaxy')
我期望的输出应该是: True
(因为单词中会出现元音,因此是 True)
然而,什么也没有发生。
您正在定义函数search4vowels()
,但 Visual Studio Code 不知道如何运行它。
尝试将其添加到您的代码中:
def search4vowels(word):
"""Display any vowels found in a supplied word."""
vowels = set('aeiou')
found = vowels.intersection(set(word))
return bool(found)
# This tells your code to run the function search4vowels():
if __name__ == '__main__':
print(search4vowels('your word'))
这是关于"__main__"
一词的手册页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.