[英]Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE?
I am currently learning Python and have come across a very strange problem (to me at least).我目前正在学习 Python 并且遇到了一个非常奇怪的问题(至少对我而言)。 When I type this code:
当我输入此代码时:
def search4vowels(word):
"""Display any vowels found in a supplied word."""
vowels = set('aeiou')
found = vowels.intersection(set(word))
return bool(found)
in an IDLE edit window and run it using IDLE, it all works fine and I need to type my function in at the prompt in order for it to start.在 IDLE 编辑窗口中并使用 IDLE 运行它,一切正常,我需要在提示符下输入我的函数才能启动。
However, when I write the same code in Visual Studio Code and run it using the Run Python file in Terminal option all I get is:但是,当我在 Visual Studio Code 中编写相同的代码并使用Run Python file in Terminal选项运行它时,我得到的是:
I have also tried the Code Runner extension and that gives me a blank output page and no way to type in the function.我也尝试过Code Runner扩展,这给了我一个空白的输出页面,并且无法输入函数。
For example : search4vowels('galaxy')
例如:
search4vowels('galaxy')
And the output that I expect should be: True
(Because a vowel would be present in the word, therefore True)我期望的输出应该是:
True
(因为单词中会出现元音,因此是 True)
However, nothing happens.然而,什么也没有发生。
You are defining function search4vowels()
, but Visual Studio Code doesn't know how to run it.您正在定义函数
search4vowels()
,但 Visual Studio Code 不知道如何运行它。
Try adding this in your 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'))
Here is the manual page about the word "__main__"
.这是关于
"__main__"
一词的手册页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.