简体   繁体   English

为什么我的 Python 代码在 Visual Studio Code 中没有正确运行,但在 IDLE 中运行良好?

[英]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选项运行它时,我得到的是:

IDE截图

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.

相关问题 我的代码在python shell中运行良好,但在我的Visual Studio代码上运行它时没有显示任何内容 - My code works fine inside the python shell, but not showing anything when running it on my Visual studio code Python计划脚本在IDLE中运行,但不在Visual Studio Code中运行 - Python schedule script running in IDLE but not in Visual Studio Code 代码在python IDLE中工作但在visual studio中没有? - Code working in python IDLE but not in visual studio? Python elif 适用于 IDLE,但不适用于 Visual Studio Code - Python elif works in IDLE but not Visual Studio Code 为什么我的 python 代码在命令提示符下运行良好,但在 sublime 中运行不正常? - Why is my python code is running fine on command prompt but not running in sublime? 为什么此导入代码在 Visual Studio Code 中有效但在 IDLE 中无效? - Why does this import code work in Visual Studio Code but not IDLE? Difficulty running a Python code in Visual Studio Code, runs in the Python shell 但不是这里 - Difficulty running a Python code in Visual Studio Code, runs in the Python shell but not here 我有一个可以在python空闲状态下完美运行的代码,但未在终端中正确运行 - I have a code that is running perfectly in the python idle but it is not running in the terminal correctly TKinter 应用程序在 Visual Studio Code 中运行良好,但作为 an.exe 崩溃 - TKinter application runs perfectly fine in Visual Studio Code but crashes as an .exe Visual Studio 代码未运行 Python 3 - Visual studio Code not running Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM